 Hello and welcome back. In this session, we are going to study about inlined member functions and about templates in C++ as a prelude to our study of the standard C++ library. Here is a quick recap of some of the relevant topics we have already studied. We have studied about object oriented programming, at least some important aspects of it with structures and classes. And in this study, we have basically seen self-contained definitions of classes as well as structures. What do I mean by self-contained? It means that all the member functions were completely defined within the boundaries of the class definition. In this lecture, we are going to study about inlined and non-inlined member functions of classes. And we are going to see further use of the scope resolution operator that we had seen earlier. And we are also going to talk about template classes and functions. Much of this lecture is motivated by the treatment in the book and introduction to programming through C++ by Aviram G. Ranade, published by McGraw-Hell Education in 2014. All examples taken from this book are indicated in the slides by the citation AGR book. Now, what do we mean by a self-contained class definition? Here is the class V3 for representing a three dimensional vector that we have studied a lot in our previous lectures. Now, in this class, there are some data members and there are some member functions, some of which are private, some of which are public. Here, of course, this is the constructor function and this is the destructor function. The destructor function pretty much does nothing here, simply returns. But note that this private member function is completely defined within the definition of the class V3. Similarly, the constructor function and the destructor function are completely defined within the definition of the class V3. I have actually written down the exact instructions that are going to be executed in this constructor function, in this destructor function, in this length function. The exact definition of the function exactly what instructions are going to be executed have been specified within the class V3. And similarly, some other operator overloading member functions, they are also public member functions of the class V3. We have seen these in our earlier lectures. But note that these operator overloading functions are also completely defined within the class V3. Such class definitions are also called self-contained class definitions. Everything that you need to know about the class is completely contained within this opening brace and the closing brace of the class definition. Now, can we always write self-contained classes? Well, if member functions are defined completely inside the class definition, like all the member functions we have seen so far, then we call these member functions inline member functions. And it is convenient to write inline member functions, if the function definition contains only a few lines. However, if you have long and complicated member functions, then it may not be very convenient to define the member functions completely within the opening and closing brace of the class definition. C++ actually does not require all member functions to be completely defined within the opening and closing brace of the class definition. It allows member functions to be declared in a class definition, but actually defined outside the class definition. And as we will appreciate, this is indeed very useful if different member functions of a class are being developed by different members of the same team. So, different members of the same team could actually continue writing the definitions of the different member functions outside the class definition, perhaps even in different files which can then be put together to form the overall definition of the class with the different member functions developed and defined by different team members. So, here is how we might do it. Here is the same class V3, the same private data member, but note that this member function is not defined here. I have not stated what exactly this member function does. I have just declared it. Similarly, this member function which is actually a constructor function has also not been defined here. It has simply been declared here. So, is this member function and this member function. However, the destructor member function has been completely defined here. So, this is an inline member function definition whereas, these are non inline member function declarations. And note that I can have a mix of non inline member function declarations and inline member function definitions within the boundaries of the class definition. Now, I have only declared these member functions here. Where am I going to define them? Well, I could define them outside the boundaries of the class definition, outside the opening and closing braces of the class definition, perhaps in the same file or perhaps even in a different file which can be included later on along with the file that contains the class definition. Now, when I am defining these non inline member functions outside the boundaries of the class definition, there is a particular protocol, there is a particular convention that I have to follow and that convention is written here. I of course, have to write the name of the member function. Here it is the constructor function v3, but I have to add before it the scope resolution operator and the name of the class. So, this is basically saying here is the definition of the member function v3 of the class v3 and this is exactly the definition that we want. Here is the definition of the member function operator plus of the class v3 and this member function returns an object of class v3 and here is the definition of this particular member function. Similarly, this is the definition of the member function operator star of the class v3. The scope resolution operator basically tells us that this member function is of this class and of course, this member function of this class is going to return an object of class v3. So, that is what this v3 denotes and here is the definition of this member function and finally, here we have the last member function definition, the member function length of the class v3 returning a value double and here is the definition of this particular member function. Now that we know how to write class definitions by perhaps declaring some member functions inside them and then defining them outside them and also some member functions could be defined inside the class as well. Let us see what might happen in a typical programming environment where I might want to use different classes which do not differ very much from each other, but differ only in the data types of some members. So, here is the class for implementing a queue of integers. Let us say that these integers are identifiers of cars waiting in a queue. Now in this class, I have several data members and we have seen a brief glimpse of this class earlier in an earlier lecture. So, I am not going to go into the details of the various member functions of this class. I am just going to say that this class has certain data members. One of these data members is an array of size 100. This is an array of integers and these data members are also of integer type. It has several member functions. This is a constructor function. This is a destructor function and there are some insert and remove functions. Note that the insert function takes a parameter which is of the same data type as this queue of a particular data type which is integer. Similarly, the remove member function returns a value of a particular data type which is the same data type as the data type of the queue. The queue is basically a queue of integers. Now here, I have shown the definitions of these two particular member functions in certain remove. Of course, these are non-inline member functions. So, I have defined them outside the class boundary and I have used the scope resolution operator. Now what I want to ask is that suppose after having defined this class of which is a queue of integers, suppose I also want to define a queue of three dimensional vectors, basically a queue of the v3 class that we have already studied. Now I could go back and try to define this v3 class and what I would realize is that this definition of the class is very very similar to the definition of the class int queue that we just saw. The only difference is highlighted here in red. I am perhaps going to give a different name of the class. The elements are going to be of the elements array is going to be an array of v3 objects rather than integers. The constructor and destructor function names will be different because the name of the class is different and the insert and remove member functions will take arguments which are of class v3 rather than integers. So, this is really the only difference between the integer queue class we saw earlier and the v3 queue class that we are seeing now. How about the definitions of the member functions? Well, they are also almost exactly the same. In this particular case, the only difference in the definitions of the member functions are of course, the name of the class before the scope resolution operator and the type of the parameters and that is pretty much it. That is the only difference between these two queue classes. Well, since the differences between the int queue and v3 queue classes are only with respect to data types of some members, wouldn't it be really nice if we could define a template for a queue class with a generic data type t and then I could instantiate this template by specifying t to be integers in one case by specifying t to be the v3 class in another case or I could even specify t to be some other class like double and then I would get queue classes for different data types. So, wouldn't it be really nice to be able to define such templates? Now, it turns out that C++ actually provides us a mechanism for doing this and this is called a template class. Now, template classes are also considered the foundation of generic programming which is a term used to denote programming independent of specific types. Template is basically a schema for creating several classes that differ only in the data types of their members and in the values of some parameters. So, basically templates allow us to give an abstract definition of a class. The definition of the class is complete with data members and member functions. However, the types of several data members and member functions are going to be generic data types rather than specific data types. So, here is how a template class for the queue that we wanted to define might look like. So, note that here I have basically defined the class queue as I had done earlier except that I have used a generic type t for denoting the type of elements in the queue and I have prefixed the definition of the class queue with template opening angle bracket class t closing angle bracket. So, this is what says that this is a template class and the templatization the generic data type that I am using is with respect to this class t. Now, how are the member functions of this template class going to look like? Well, they are going to be called template member functions and as you can see they are pretty much the same that what we had seen earlier. I am going to use the generic data type instead of the specific data type. However, instead of saying queue colon colon I have to say queue opening angle bracket t closing angle bracket colon colon. Basically, queue which is a template class and the abstract data type or the generic data type I am using there is type t. And of course, I have to prefix this entire member function definition by template opening angle bracket class t closing angle bracket. So, this basically tells the compiler that I am using a template member function. This class t that I am using here is also the class with which I am going to instantiate the template for the class queue and then this is the definition of the member function in which I am going to use this generic data type. Similarly, the other template member function would look exactly the same I have to prefix it with template class t. The same class t is going to be used to instantiate the template class queue and in the definition of the member function I am using the same generic data type t. And how am I going to instantiate these template classes? Well, perhaps in this way where I say I want the class queue which is really a template class, but I want to instantiate and create a specific instance of this class where the generic data type t is going to be replaced by int. And here is an object of this particular instantiated template class. The instantiated template class is of course, just like an ordinary class where the generic data type t in the definition of the template class has been replaced by int. Similarly, my v 3 q object is an object of the instantiated template class q where the instantiation of the generic data type t has been done with the class v 3. So, you see that by using template classes I can help reduce repetition of code. This indeed facilitates generic programming. It allows us to think at a more abstract level I can think about q of a generic data type rather than q of an integer or q of class v 3. And as we will see next when we studied the C plus plus standard library that using templates is crucial when we use the C plus plus standard library. Here is a concluding note about template classes. A template class definition not only allows you to use one generic data type, but you could use multiple generic data types and you could use also other parameters using a comma separated list. So, for example here is the same template class q where I have used a generic data type t plus t. And I have also used an integer parameter called q size. And in the definition of the class I can use this integer parameter for example to define the size of the array elements where each element of the array is an object of the generic data type class t. And when I instantiated I have to specify values for t as well as for q size. So, I could say here is an object my v 3 q object which is of type q instantiated with generic data type t replaced by v 3 and q size replaced by 10. Here is my integer q object which is an object of the template class q instantiated with the generic data type t replaced by int and q size replaced by 100. So, you can see that by allowing more parameters I indeed have greater flexibility in defining two different objects of two different q classes, but both of which are instantiated from the same template. So, in summary in this lecture we studied about inline and non-inline member functions of C plus plus classes. We saw the use of the scope resolution operator in defining non-inline member functions. And we also studied about template classes and functions. Thank you.