Hey guys, what's up, this is Umar Khan and I welcome you to a new section here on this course on advanced C++ and in this course we are going to be studying about virtual functions. Now this is going to be the content which we are going to be covering in this section that includes virtual functions and runtime polymorphism, default arguments, derived classes, virtual constructors and destructors or TTI, inline and pure virtual functions and we are going to be covering some important questions about virtual functions and finally we are going to be covering a lot of examples. Now to elaborate further in the virtual constructor and destructor part we are going to be covering virtual constructors, virtual destructors and pure virtual destructors. Then in the inline and pure virtual function part we are going to be covering about inline virtual functions, pure virtual functions and we are going to see the difference between pure and normal virtual function. And then in the part of some questions about virtual functions we are going to be covering two things, can a virtual function be static and can a virtual function be private or not? And finally we are going to be covering a lot of example on each and every topic. So there is so much for you guys to learn in this section so I hope you stick to this section and learn each and everything. So thank you so much up till now in the next tutorial we are going to be formally starting on with this section. So bye-bye till then. Hey guys what's up this is Amir Khan and I welcome you to the very first tutorial on virtual functions and in this tutorial we are going to be covering the basic introduction to virtual functions and we are going to be covering about runtime polymorphism in virtual functions. So starting on with the introduction to virtual functions. Now a C++ virtual function is actually a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function. There is a necessity to use the single pointer to refer to all the objects of the different classes. So we create the pointer to the base class that refers to all the derived objects. But when base class pointer contains the address of the derived class object it always execute the base class function. This issue can only be resolved by using the virtual function. Now a simple virtual keyword is used to declare a virtual function. A virtual is a keyword actually preceding the normal declaration of a function. So when the function is made virtual the C++ compiler determines which function is to be invoked at the time of runtime based on the type of the object pointed by the base class pointer. Now to talk about the syntax of a virtual function it is right here on your screen. You have only to write in this virtual keyword then the return type of the function that is going to be the normal type that can be void that can be integer that can be float or a character or anything else you want to. And then the normal function is just followed in just as that in which we have the name of the function then the arguments if there are any and then the definition of the function. So the only difference between a virtual function and a normal function is in just general or the difference that you feel just by seeing is that it has an additional keyword that is right in here. It has additional jobs but at the first glance the first difference you note between a normal function and a virtual function is that a virtual function will always have this keyword that is the virtual keyword. So now what is a late binding or a dynamic linkage? In late binding the function call is resolved during runtime therefore the compiler determines the type of the object at runtime and then binds the function call. Now there are some rules of virtual functions that always have to be followed. The first rule is that virtual function must be a member of some class. The second rule is that virtual function cannot have static members. Third, they are accessed through object pointer. Four, they can be a friend of another class. Next one is that a virtual function must be defined in the base class even though it is not used. Next one is that the prototypes of a virtual function of the base class and all the derived classes must be identical if the two function with the same name but different prototypes is used. C++ will consider them as overloaded functions. Next rule is that we cannot have a virtual constructor but we will have a virtual destructor. Now why we have a virtual destructor but not a virtual constructor? This is covered in very detail in the future tutorial. Now let me just show you a quick example in which we will cover an example with and without the virtual keyword. So then you will feel the difference that what is actually the difference that comes when you are dealing with base classes and derived classes. So let's move on to our compiler right in here and right in here what we are going to do is that we are going to define a normal class that is class A and then right below this class what we are going to do is that we are going to have a class B that is going to be derived from class A. So we are going to write in public A. So this means that this class B is actually derived from this class A. So this is the body of that class. So now what in these both classes we are going to do is that we are going to simply make some function named as display and then using the objects you are going to feel the difference that how using the virtual keyword is going to solve some problems in dealing with base classes and derived classes. So first let's code these two classes and make functions in them without using the keyword of virtual. So we are going to make a public function in both the classes named as simply display and as I told you that we are going to use this display function without the argument. So we are going to simply write in what display and we are not going to use the building keyword virtual. So these are not going to be virtual function and then you are going to see the output. So we are going to simply see out this is the base class and then now right in this function or in this class B that is derived from class A. What we are going to do is that we are also going to make a public function that is going to have the very same name that is display and in the body of this function what we are going to do is that we are going to simply see out this is the derived class. So simple enough we have just made two classes in which this is the class A and then this is the class B that is a derived class of from this class A and then both these have the same function that is the white display function that are public and this has simply see out this is the base class and this has simply see out this is the derived class. Now if you just move on to code the main function that is right down here what we are going to do is that we are going to make an object pointer of class A that is the base class and then we are going to make a simple object of class B. Now what we are going to do is that we are going to make a reference from A that since this is a pointer so what we are going to do is that we are going to write in A equals to NB which means that this object pointer of this class that is the base class is pointing towards the address of this object that is that an object of the derived class of this class A. So what we are going to then do is that we are going to write in A dot display return zero. So now if you just run this code all right it says request for member delay in A yes this has to be something like this now let's just run it and you can see that it has printed out this is the base class. Now in this example actually this steric A is the base class pointer. The pointer can only access the base class members but not the members of the derived class. Although C++ permits the base class pointer to point to any object derived from the base class but it cannot directly access the members of the derived class therefore there is a need for virtual functions which allows the base pointer to access the member of the derived class. Now in this case as you can also see that we are actually trying to access this display function that is from the derived class using the object of this but this is actually not allowed in simple functions. Now if you code this very same scenario using the virtual functions that what we are going to do is that we are going to simply place virtual right in here and make this function as virtual wide display. This is going to be simply wide display and in the main function what we are going to change is that we are going to actually do nothing but now what is this actually this is actually the pointer of the base class this is an object of the derived class this is the referencing and then this is actually where the late binding occurs. So this is the area where late binding occurs and you know what is late binding so since this is the late binding and now in this case actually this is a virtual function so now if you run this you can see right here that it has printed this is the derived class so the whole output has been changed in the previous case it was printing this is the base class but now it is printing this is the derived class in the previous code this main function was the very same we were using this pointer of the base class and referencing it towards the object of the derived class and then calling the display function but it was not allowing allowing us to call this display function but now when we have used the virtual keyword what it actually does is that it is now allowing us because the late binding has occurred and late binding then allows you to access the members of the derived class so now this is basically the difference that comes when you use the virtual keyword and that's why the concept of virtual functions was introduced into c++ so that the objects and pointers object of the base class can be used to reference or access any members of the derived class so I hope it is clear now so I guess that's it with this tutorial as well in the next tutorial we are going to be covering more on virtual functions so for now thank you so much for watching and I'll see you guys in the next tutorial hey guys what's up this is Abir Khan and I welcome you to another tutorial on virtual functions in this tutorial you will see how default values work for virtual function as they do for normal function now I hope you know already about what are a default arguments to a function and how it works so default arguments actually do not participate in the signature of a function so signatures in base class and derived classes are considered same hence base class function is actually overridden default value is used at compile time so when compiler checks that an argument is missing in a function call what it do is that it substitutes the default value given so now let's just simply see that how does this stuff work for virtual functions with the help of an example so let's move on to our editor and what we are going to do is that we are going to make two classes one is going to be the base class and then one is going to be the derived class so we are going to have a class a as we have in our previous tutorial as well then what we are going to do is that we are going to have class b this is our class b but this class b is actually going to be a derived class of this class a so what we are going to write in we are going to add a colon and then we are going to write in public and then the name of the class from which it is derived so it is derived from class a so class b colon public a is going to be written right in here now what we are going to do is that both these classes are going to contain a same function and this class a actually is going to have a public function that is going to be virtual function so we are going to use the building keyword that is virtual then white s that is the name of the function white is the return type and what basically we are going to do in this is that we are actually going to check for default arguments in virtual functions so let's just add a default value that is in a equals to zero and in the body of this function what we are going to just see out is that we are going to see out this is the base class all right now in the second class that is class b that is the derived class and that is derived from class a what we are going to do is that we are going to make a public function that is also going to be virtual and its name is also going to be the very same and in this we are going to be receiving in a in this we are also receiving into a but this was made as a default value so when nothing is passed to this it is going to itself consider that value to be equal to zero so now you are going to actually feel the difference that what is going to be that what is going to be called when no value is passed and what is going to be called when some value is going to be passed so you're going to see the difference just in a moment so in this function what we are going to do is that we are going to see out this is the derived class and one more thing what we are going to do is that we are also going to let's say see out the value and we are going to just write it in here this is the derived class and the value of a is and then we are going to simply write in a here and then end line now in the main function what we are going to do is that we are going to make objects now right in here what we are going to do is that we are going to make an object of the derived class that is b then we are going to actually make a pointer of the class b that is actually pointing to whatever was in her in this case we are going to make a pointer of class a actually and what we are going to do with the pointer of class a is that we are going to make it point to this object of the derived class so we are going to write in a static a is going to be the name of the pointer and what we are going to make it equal to is that we are going to make it point to this so we are going to write in and b equal to and b so this is the very same case as we were discussing in the previous tutorial that how this is going to reference from this pointer towards the pointer of the derived class so now what we are going to do is that we are going to call in the base class actually so how it is going to be done that we are going to use this pointer that is a and we are going to call in the function s so if we just write in return zero and run this code you can see that it has printed this is the derived class and the value of a is zero now what does happen actually here as you can see that we passed in no argument as you know by the definition of virtual functions that virtual functions allow us to actually access the members of the derived class so in this case since we have used the pointer of the base class and pointed it towards the object of the derived class that is b right in here and we have called the function s so it is supposed to call this function that is that says that this is the derived class and now why it has printed the value of a equal to zero but we haven't passed anything it is because it actually used this value right in here that was int a equals to zero so in this output we observe that the s function of the derived class is called and the default value of the base class s is used that is right in here at the top that is int a equals to zero so it printed out this line that is this is the derived class and the value of a is and then for the value of a since we passed in nothing so what it does is that it use this value right in here that was the value in the public function the public virtual function i must say of this class a that was the base class so default arguments actually do not participate in the signatures of functions as i was telling you before so the signature of the function s in the base class and derived class are now considered to be the very same hence the base class s is actually overridden default value is used at compile time so when compiler checks that an argument is missing in a function call as it is in this case what it do is that it substitute the default value given that was given right in here at the top in this virtual function so since this function was overridden so it used actually this value therefore in this program value of a is substituted at compile time and at runtime the derived class of class s function is called so the value of a is substituted at compile time and at runtime derived class s function is called so if you just pass in some value let's say let's say pass in two to this and now call this you can see right here that it says now that this is the derived class and the value of a is two now good so now since we have passed in some value so what it's going to do is that it's not going to overwrite this function and not going to use this value that was from the base class so now it is going to use its own value and it is going to print two in in the space of this a because it is going to receive this two here and print it out right in here so I hope it is clear so now let me give you one more example and show you how things remain the same when the default values are used for virtual function so for example we have again two classes and let's be these two classes the very same that is class a and class b and and in these what we are going to do is that we are going to have public white s function and then int a equals to zero is going to be the very same and then we are going to print out this is the base class and what we are going to do is that we are actually going to call the function that is right in here so what we are going to do is that we are going to write in a over here and simply see out it and the rest is going to be the very same and let's just remove this value from here as well so if you just run it now you can see that it again says this is the derived class and the value of a is zero since we also printed out the value of a up there right here at the top but no difference was found in so if you just take a closer look at the output you observe that the s function of the derived class is again called and the default value of the base class is again used so the default arguments basically do not participate in the signatures of the functions again i'm telling you this so the signature of s in the base class and derived class are considered same hence the s of the base class is over hidden also the default values used at compile time so when the compiler sees that an argument is missing in a function call it substitute the default value given therefore in this program again the value of a is substituted at compile time and at runtime the derived class s is called basically so now let's see the third scenario in the third scenario let's say we have this very same class a and then class b that is the derived class class a is the base class and what is going to be the difference is that we are also going to supply some value right in here to this function that is int a equal to 2 so now as you can see that the base class function s has a default value the derived class also has a default argument right in here that already have a value it also has a value int a equals to 2 and this has a value of int a equals to 2 so now if we use the very same concept here in the main function that is an object of the derived class then an pointer object of this a class pointing towards the reference of this object of derived class that is b and then calling the s function and returning zero and passing nothing to this function and now if you just run this code and let's just observe the output now as you can see that it again says that this is the derived class and the value of a is zero so the output of this program is the same as the previous program and the reason is also the very same the default value is substituted at the compile time the s function is called on the pointer which is a pointer to the base class type so the compiler substituted zero instead of the two which was the default value of this function so always remember the lines I have spoken twice that is default arguments do not participate in the signature of a function so whenever we have we use this reference and pass in nothing to this function then it's never going to use this value whether it is int a only that was for case one and case two or in case three if you supply even a value to this function it is not going to use it because it is going to use this value right in here and the reason as I told you is the very same that default value is substituted at compile time so the function s is called on the pointer of this class right here that is class a sorry not class a the pointer a of the class capital a and it is substituted which is a pointer of a base type actually so the compiler will substitute the value of this pointer that is the base class not of this whether it is default or it is not so in general it is best practice to avoid default values in virtual function to avoid confusion because in this program you were you would have been expecting that it would now print two since this has a default value now but it is going to do that not because if you supply a value to it and you run this code then it is going to use this value three that was passed in as you can see right in here but if you pass in nothing then every time it is going to go for the base class and grab its value right from above so I hope it is clear with these two three examples so I guess that's it with this tutorial as well now you know how to use default arguments and what virtual functions are going to do when arguments are not passed to it and it uses default arguments so I guess that's it with this tutorial as well thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Han and I welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be covering virtual function in derived classes so we know that when virtual functions are declared virtual in the base class it is mandatory to redefine virtual functions in the derived classes now the compiler actually creates v tables for the derived class and store the addresses of the functions in it in case the virtual function is not redefined in the derived class the v table of the derived class contains the address of the base class virtual function thus the v table contains addresses of all the function thus it is not possible for a function to exist but for its address to not be present in the v table so let's see an example in which we will show the possibility of a function existing but an address not being located in the v table so let's move on to our compiler so right in here what we are going to do is that we are going to again define two classes class A and class B class A is going to be the base class class B is going to be a derived class from class A and then both are going to have the very same function that are going to be virtual functions so let's just do it so first we will have a class A and then we will have a class B that is derived from class A and both are going to have a function that is a virtual function that return type is going to be void and let's say the name of the function is s again so what we are going to actually see out in this function is that we are in the function of the base class now to move on to this class B that is the derived class we are again going to make a public function that is going to have the very same name that is void s and in the body of this function what we are going to do is that we are going to simply see out we are in the function of the derived class simple enough and now what we are going to do is that we are going to make one more function here that is going to be void and it is going to be virtual as well so and the name of the function is now going to be something different let's make it equal to s1 or let's say sA and in the body of this function what we are going to do is that we are going to simply see out we are in the function sA of the derived class and l and let's make it we are in the function s of the class of the derived class and in this function it's going to say we are in the function sA of the derived class so in the main function down here what we are going to do is that we are going to make two objects of the class A that is A1 and let's say A2 and then what we are going to do is that we are going to make a simple object A3 of the very same class A that is the base class and now we are going to make an object of the class B and then using the first object that is A1 what we are going to do is that we are going to reference it towards A3 that is actually the address or you may say object of this class A which means that A1 is pointing towards this class A so using the second object that is A2 what we are going to do is that we are going to point it towards this object so we are going to write in end B and then using these two objects if you write in A1 and call in the function s not scientific and then using the A2 object if you call in the very same function that is s and now if we just run this code so it says that we are in the function of the base class and then it says we are in the function s of the derived class as you can see right in here since A1 is actually pointing towards A3 and what is A3? A3 is actually an object of class A so it basically called this function that is we are in the virtual function of the base class and then A2 was pointing towards the object of the derived class so it printed out we are in the function s right above here of the derived class now if you just use the very same object A2 that is actually pointing towards the derived class and you write in sa let's say that was the second function we have used and run this you are going to see it is going to generate an error and the error is going to be that class A basically has no member that is named as sa so in the in this program actually the base class A contain one virtual function that is the function s in the derived class basically we have defined in two function that is the s function that is redefined and it is defined as a normal function not a virtual function and then we have defined a new virtual function sa that is a virtual function so so now the v tables that are created for the base class and derived class are going to be very interesting the base class v table is actually going to point towards the s function that is present in the class A and the derived class v table will have only one function that is the s function so this statement right in here that is A2 referencing referencing towards sa will generate an error message in this statement the pointer A2 is actually treated as the only pointer to the base class object now the function sa basically is actually not going to be the member of the base class A hence it is not allowed to invoke the function sa using the pointer object A2 however an address of the derived class is assigned to the base class pointer and the compiler has no way to determine that we are looking with a derived class object so the compiler avoids spoiling calling the virtual function present only in the derived classes in a hierarchical class organization of various level if it is essential to invoke a function at any level by using a base class pointer then the function should be declared virtual in the base class so I hope it is clear so one more thing you need to know is that in C++ once a member function is declared as a virtual function in a base class it becomes virtual in every class derived from that base class and this is also actually what happened right in here in this example we just covered so in other words it is not necessary to use the keyword virtual in the derived class while declaring redefined version of the virtual base class function so for example if you just consider an example let's just remove this quote from here this function from here and this has to be the very same you can see right here that we have defined a virtual with this white s function so now re redefining of this function that is white s you do not have to specify a virtual with this function for example if we have one more class let's say class C and that class C is actually derived from class B we do not have to write in class B we have to write in public B and in the body of that we also have a public function that is also white s we do not have to write in S with this as well so if you write in C out we are in the function of class C and L and let's make it we are in the function of class B and then we are in the function of the base class good enough so now to move on towards this main function we'll just remove this from here and for example we have an object C of a class C that is and then we have an object pointer of class B and it is referencing towards class C since class C is actually derived from class B so B pointer is going to reference towards this C pointer and then we if you write in B and call in the function S not scientific why it is writing scientific every time this function S and use return 0 and run this code all right it says that we are in the function of class C we have not defined the word virtual with this not a virtual with this but we have used it with the virtual right at the top here so when these functions are redefined in the derived classes that are class B and class C since class B is derived from class A and class C is derived from class B so this is also a derived class so anything derived from class A and further from some class that is derived from class A is going and it's containing this function YS and we have not defined the word virtual with it it is going to be virtual by default so that's why when we created an object of this class C and then using this class B object we have actually pointed towards the reference of this class C object and when we called in this function S so when I showed you the very first example when we started on with this section I showed you that how not using the virtual function is going to not let us to call the function of the derived class so in that case if you remember the function of the base class was printed actually in this case class B is the base class if we consider only these two classes class B and class C so for class C class B is the base class it is a derived class for class A but for class C it is actually the base class so if you just ignore this and you say that virtual has to be defined with each and every function then the output should have been we are in the function of class B but now as you can see on your output screens that it says we are in the function of class C right here it says we are in the function of class C which means that these functions are also virtual and that's why it is this referencing is allowing us that laid binding and allowing us to actually access the members or you may say functions of the derived class so in this case class C object was made B pointer object was used to reference to this object and then when we call on this function of this class actually it called the derived function and why it has because these functions are also virtual by default so I guess that is clear and that's it with this tutorial as well so thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on virtual functions in C++ now in this tutorial we are going to be covering a very interesting topic in which we have to answer a question that can static function be virtual or not now to answer this question and to understand it first you need to know the difference between a static function and a virtual function now a virtual function is a class member function that you expect to be redefined in derived classes when you refer it to a derived class object using a pointer or a reference to a base class you can call a virtual function for that object and execute the derived class version of the function you need at least two classes to see a difference now functions in derived classes actually override virtual function in base classes only if their type is the same you do not actually overload virtual functions now a function in a derived class cannot differ from a virtual function in a base class in its return type only the argument list must differ as well so when calling a function using pointer or references some rules have to be followed now what are those rules actually the first rule is that a call to a virtual function is resolved according to the underlying type of object for which it is called the second rule is that a call to a non-virtual function is resolved according to the type of the object or reference now since virtual functions are called only for object of class types you cannot declare global or static functions as virtual now the virtual keyword can be used when declaring overriding functions in a derived class but it is unnecessary as it overrides of a virtual function are always virtual virtual function in a base class must be defined unless they are declared using the pure specifier that are used in declaration of comm interfaces now the virtual function call mechanism can be suppressed by explicitly qualifying the function name using the scope resolution operator to call specific version now you already know about v tables and how calls to virtual functions are resolved using that so this was about virtual function you already know about virtual functions in very detail because we have covered three to four tutorials already on virtual functions and we have also covered examples on how virtual function works how virtual functions are overridden and you already know what i am talking up till now so now let's talk about static functions so when modifying a member function in a class declaration the static keyword specify that the function is considered to have a class scope in contrast to non-static member functions static functions have no implicit this argument therefore they can use only static data members enumerators or nested types directly static member function can be accessed without using an object of the corresponding class type now as a result of this some restrictions may apply to such function first they cannot access non-static class member data using the member selection operators second they cannot be declared as virtual third they cannot have the same name as a non-static members that has the same argument types now coming towards the main question that is that can a static function be virtual so the answer is a big no why because virtual functions are invoked when you have a pointer reference to an instance of a class static functions are not tied to a particular instant they are tied to a class c++ does not have pointer to class so there is no scenario in which you should invoke a static function virtually now in c++ a static member function of a class cannot be virtual so let's see an example of what i've been talking so far so let's move on to my compiler so for example we have a class a then we have another class that is class b and this class b is a drive class so we are going to write in public a write in here and then this all has a public function that is a virtual function and also static as well so at the end you are going to see that how this is going to generate error so let's say the name of the function is s and let's see how anything because it is not going to be printed out all right so let's suppose in this class b we again have a public function that is let's say white s and it is again static and as you know from the previous tutorial that it is going to be defaulted by default it is going to be virtual so in this function let's see out oh let's just print out commas all right so now if we just make an object and then using that object what we do is that we actually make a reference to the drive class and then we write in we just run this code yes we have error write in here and that says that the member s cannot be declared both static and virtual which means that if a function is virtual it cannot be static at the very same time it can be static but it cannot be virtual then but if it is virtual then it cannot be static both cannot go in parallel with each other you can do one with any of them you can make this s to be a static function or a virtual function but not both it cannot be declared both static and virtual so this is going to generate error so that's how our question has been now answered that can static function be virtual the answer is a big no moreover if you talk about static functions then one thing about static function is that static function cannot be constant and volatile for example if this is not a virtual function let's say and for example this is a static function and moreover what you do is that this is static white s and then what you do is that you and constant write in here so if you just run this code now it is again going to generate error and then it says that static member function static white a s cannot have cv qualifier this is because a static function cannot be constant and volatile so a static function cannot be virtual and a static function cannot be constant and to talk about virtual function virtual function cannot be static so the answer to this question is that can static function be virtual or not the answer is a big no and i've already told you about the reason that why it is so because virtual functions are invoked when you have a pointer reference to an instance of a class static functions actually are not tied to a particular instance they are tied to a class actually so c++ does not have a pointer to class so there is no scenario in which you could invoke a static function virtually so you now know the answer to this question you have understand the difference between static and a virtual function you have covered examples and i guess that's it with this tutorial as well thank you so much guys for watching i'll see you guys in the next tutorial hey guys what's up this is umar han and i welcome you to another tutorial on virtual functions in c++ now in this tutorial we are going to be learning about virtual destructors so destructors in the base class actually can be made virtual when whenever upcasting is done a destructor of the base class must be made virtual for the proper destruction of the object when the program runs so now we will see examples in which we will first see upcasting without virtual destructors and then we will see upcasting with virtual destructors and in those examples you are going to feel the difference that what is going to be the difference so let's move on to our editor and in here what we are going to do is that as usual we are going to have multiple classes so first we are going to have class a as usual and then we will have a class b that is derived from class a this is the body of that and now what we are going to do is that instead of public functions in here what we are going to have is that we are going to have a public destructor so whenever a public destructor is used you actually have to use this sign writing and then you have to write the name of the class in this case it is going to be a and the brackets and then the body now in this example we are going to be actually doing upcasting without using the virtual destructors so in this what we are going to write in see out this is the base destructor and in the second class what we are going to write in it is also going to have public and then this time it is going to be b the brackets the body and we are going to simply see out derived class destructor now if we just move down here into the main function first we have to make an object of the base class that is actually upcasting on towards the derived class so we are going to write in a that is the base class then we have to make an object let's say a is the name of the object then we are going to write in new and then we have to write in the name of the derived class so this is actually upcasting and then what we have to do is that we are going to simply write in delete a so this in this example actually the call that is delete a will only call the base class destructor which is undesirable because then the object of the derived class remains undestructed because its destructor is never called which actually results in memory leak if you consider the very starting examples which we have been covering on virtual functions you know that when virtual functions are not declared actually the stuff in between the derived class or the members of the derived class are actually not allowed to be accessible so in this case this is actually never called so this result in memory leak and the object of the derived class can never be destructed so if you just run it you can see right here that it says base destructor which means that the base destructor has actually been destroyed and the base class objects have been described but the object of class B that is the derived class will never be accessed now if you do the same upcasting but is this time what we are going to do is that we are going to be using the virtual destructor not a normal destructor this is actually a normal destructor so to use that what we are going to do is we'll add a virtual in here and then this is going to actually be a virtual destructor so the rest is going to be the very same this is the upcasting done in the very same way delete a called in the very same way but this time if you run this code you are going to see first a derived class destructor and then base destructor so when we have virtual destructor inside the base class then first the derived class destructor is called and then the base class destructor is called which is the desired behavior which is exactly what we want because we want all the objects to be deleted we want the objects of class A as well as the class B the base class and the derived class respectively we want their objects to be deleted so in this case when the upcasting is done that is right in here this is actually the upcasting part so when upcasting is done and we have a virtual destructor right in here in our base class then what it's going to do is that it's going to first call in the derived class destructor and then the base class destructor so both the destructors are going to be called in and this is exactly what we want so this is how actually virtual destructors helps in C++ so that's it with this tutorial as well thank you so much guys for watching and I will see you guys in the next tutorial. Hey guys what's up this is Amir Khan and I welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be learning about virtual constructors now the question arises that can we have virtual constructors in C++ now the answer is no now what is the reason behind this because when an object has virtual functions it must have an associated vtable which keeps track of the addresses to the virtual functions for that class that would mean that an object must be instantiated firstly before a vtable for that object could exist since constructors are intended to instantiate and create the object there can be no virtual constructor in C++ the whole concept of virtual constructor actually does not make any sense it cannot be given a sensible meaning considering how object and classes behave in C++ a virtual function call involves a dynamic dispatch mechanism it conceptually involves looking up the actual type of the object rather than the type of a pointer or reference to that object and using that type to determine the function to be called however when you call a constructor there is no object yet before a constructor is called to initialize an object in a region of memory that region of memory has no type therefore there is nothing to look up or dispatch on instead the caller always explicitly specifies the constructor to be called this is what determine what type of object will have once initialized now another way to answer this question is that if you could have a virtual constructor or not how would you expect its behavior to differ from that of a regular constructor for example if you consider this code right here on your screen each of these three statements an object of type t is created in them and the default constructor for t is called if that constructor for a virtual then what would it be possible for these statements to actually construct object of type derived from tree rather than t itself or would you just want a different function to be called what would determine which function would really be called I think there is no sensible answer to all of these questions so the concept of a virtual constructor in C++ is actually just not coherent so let's cover an example to see if virtual constructors work so we'll have a class A and in this we have we will have a public virtual constructor and in that virtual constructor we are going to simply see out let's say it is going to say base constructor now if we have another class that is class B the derived from this class A and it also has let's say a public constructor and it is going to be virtual as well and it is going to see out derived class constructor so all you need to do in this main function is that you need to only create an object of the class A and that is going to simply generate an error you can see right in here and it says that constructors cannot be declared virtual as in the F permissive range so constructors as it told you and explain you why constructors are not virtual so I guess that's it with this tutorial so thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Amir Khan and I welcome you to another tutorial on virtual functions in C++ now in this tutorial we are going to be covering a very interesting topic that is the virtual copy constructor now an idiom that allows you to do something that C++ does not directly support is actually called virtual copy constructor you can get the effect of a virtual constructor by a virtual clone member function for copy constructing or a virtual create member function for the default constructor so let me give you a quick example of what is virtual copy constructor and how does it work actually in C++ so I will just move on to my editor so here we are now what we are going to do for virtual copy constructor is that we are first going to create two classes as usual the first one is going to be class A then the class B that is a derived class and it is derived from the class that is class A so these are our two classes now so now in this first class right here that is the class A what we are going to do is that we are actually going to make a virtual destructor then we are going to define some virtual functions and then we are going to use the copy constructor and after using that copy constructor we are going to use the default constructor as well and then you are going to see that how defining a clone is actually going to help you out in creating your constructor so right down here what we are going to do is that we are first going to create a virtual destructor that is going to be virtual negation A and this is the body for our constructor function and after that we are going to define in let's say two functions that is virtual void let's say the first function is named as A and it is equal to zero now when we make it equal to zero it is actually a pure virtual function now so let's make one more virtual function that is also a pure virtual function let's name it as B and make it equal to zero so now what we are going to do is that we are actually going to make use of the copy constructor now how it is going to be done we have to write in this virtual keyword then the name of the class which we are dealing with place a pointer right at the top here then use the function named as clone that is a constant one and it is equal to zero this is actually we are using the copy constructor at this time now what is this I will explain just in a moment so now we have let's say one more function that is also of this a steric and then this is create constant equal to zero now this is actually our default constructor and now right here in class B what we are going to do is that we are going to make some public features as well the first one is going to be from B steric then clone and then constant then one more from the B steric object that is going to be create and this is actually in this case the covariant return type and this is again a covariant return type that is also constant all right so now right behind this function what we are going to do is that we are going to actually define these covariant return types now how they are going to be defined in we have to define them using the object of the derived class or you may say the pointer object so we are going to write in B steric then the name of the class the two columns which means that the clone function belongs to this class B and we are going to write in constant not constant it is going to be constant and then we have to define the body as well here so that is going to simply return new B this and we are going to define the covariant return type for this as well that is this B steric create constant so we are going to write in B steric B and then the create function which defined that this create function belong to this derived class then constant and then the body and in the body we are going to simply return in new B that is actually going to be the constructor of this derived class now if you notice in the clone member function we have this statement that is new B and then steric this which is actually the recent pointer now this code actually call the B copy constructor and it copy the state of this pointer into the newly created B object now it will do it unless the B is known to be final which you can reduce the change of slicing by making its copy constructor to protect it now in the create function that is right in here it is also a member function the new B that is right in here what it do is that it actually call the B default constructor now if someone want to use the virtual constructor they will be using these clone and create functions and they will be thinking that if they were using the virtual constructor now for example we have a function for example that function is in let's say this class A or let's say this class B so right here for example we have a function named as any function let's name it as x and in the body of this function for example we had to first write in the name of the class B steric and then what we have to do is that we have to define an object let's say the name of the object is q and then what we have to do is that first we have to call in s clone now what does s clone do is that s clone is actually going to perform the job of the copy constructor and after a copy of the constructor what the second statement is going to do is that it's going to create your default constructor so this function will work correctly regardless of whether the first class that is the class A it is any kind of other class that is deriving the base class so the return type of the B class clone member function is intentionally different from the return type of the clone member function of B's class now this is called basically the covariant return type a feature that was not originally part of the language so if your compiler complains at the initialization of this one that is B steric clone constant then this means that you have an old compiler and you will have to change the return type to the very first class that is the base class which means that at this time actually if you see that it is actually returning new B steric this then what you have to do is that you have to actually replace this with A but my compiler is up to date so I will be using this B right in here so this is a very bit of a tricky topic so basically what you need to understand in here the main concept is that you have to actually create a clone function and what is this clone function basically this clone function is actually the use of the copy constructor and then using that what you have to do is after using this clone function you have to use the create function that is actually going to use the default constructor and then in the derived class down right here first you have to initialize B steric clone and B steric create and then let's just remove this from here and then what this is going to do is that is going to return the covariant return types that is new B steric this and what is this I've already explained and what is this new B it is actually returning the default constructor so I hope it is clear so I guess that's it with this tutorial as well thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on virtual functions in C++ in this tutorial we will see what is RTTI now RTTI basically stands for runtime type information in C++ so in C++ the RTTI is the mechanism that exposes information about an object's data type during runtime this feature can be available only when the class has at least one virtual function so it allows the type of an object to be determined when the object or program is executing so let us see an example for how this is going to be implemented and how if we don't use a virtual function in the class base class and the graph class then how it is going to generate error and how RTTI is going to help us out so let's just move on to our editor so right in here what we are going to do is that we are going to first define two classes that is class A and then we will define another class that is class B and it is a derived class and it is derived from class A now if you write in nothing between the brackets of class A and class B means this do not have any kind of derived function and what you do is that you create a pointer of the base class that is A and what you do is that let's name the pointer as A and it is equal to new and then you have to write in the name of the second class which means that you are doing upcasting right in here so then using the second class that is the class B and you create an object let's name it as B this time and make it equal to let's say a dynamic cost now what basically dynamic cost is dynamic cost is actually used for the conversion of polymorphic types so in here this dynamic cost is used for the conversion of polymorphic types so in this what we are going to write in we are going to write in B and steric which means a pointer of the second class and then what we are going to write in these brackets is we are going to write in actually this pointer right in here so we are going to write in A then we are going to use an if condition that if this second pointer that is the pointer B it is not equal to null then what is going to do is that it's going to simply see out it is working which means that the program is working at this time but if it don't which means that this is not equal to null then what it's going to do is that it's going to print out upcasting base to derive cannot be done all right so basically when this condition is true which means that this A has something now when we dynamic cast B onto A then B should be pointing to something but this will be in the case when we have a virtual function so let's just remove these spaces from here i return zero statement at the end and let's just run this code you can see right in here that it says cannot dynamic cast A of type plus A to type class B because it says the source type is not polymorphic now why it is because according to the RTTI it must have a virtual function in it so now if we just use the very same code and in the base class right in here we define a virtual function let's say it is a void function and let's name it as option and do nothing in between these brackets let's just comment it out so now the remaining code is going to be the very same but now if you just run this code you are going to see that it is going to run perfectly and it is going to print out it is working so it is still working on it let's run it again now it has run perfectly and it is saying that it is working so since in this case this dynamic cast work perfectly and this B was a dynamic casted on this polymorphic A so this B was not equal to null which means that this B was actually dynamic casted on this A so this B was not equal to null and it printed out it is working so basically RTTI is a mechanism that allows the type of an object to be determined during the program execution RTTI was basically added to C++ language because many vendors of class libraries were implementing this functionality themselves so this caused actually incompatibilities between libraries thus it became obvious that support for RTTI was needed at the language level so for the sake of clarity the discussion of RTTI is almost completely restricted to pointers however the concept discussed above also apply to references now in C++ if you just move on to the slides these were the examples we cover now in C++ basically we have three types three elements basically to use RTTI the first one is the dynamic cast operator which we just use and it is used for the conversion of polymorphic type then the second one is the type id operator and it is used for identifying the exact type of an object and then finally we have the type info class that is used to hold the type information returned by the type id operator so I suggest that you go in detail of these as well because this is something that needs to be have been covered in the your basic concept so you need to have a basic information about these three so I guess that's it with this tutorial as well thank you so much guys for watching and I will see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be again dealing with a very interesting question that is can a virtual functions be private or not now the answer is a big yes virtual functions can be private and they can be overridden by the derived classes so without wasting any more time let me give you a quick example of how all this stuff works so right above here what we have to do is that we have to define the class B then we have to define our class A and in our class A what we have to do now is that we have to make it as private and make any function let's say the function name is virtual white S and in the body what we are going to simply do is that we are going to see out this is the function of the class A all right now outside this what we need to do is very interesting now what we have to do is that we are going to write in front and then we are going to write in main now what is this I will be explaining to you it at the end of the tutorial if you don't put it here it will generate a compiler now why have I put it here I will explain at the end so we have to add a colon here as well now you have to define the class B and you have to write in public A and then what you have to do in here is that now you are going to define actually a public function so what is going to be public it is going to be the very same function we have defined right here in the base class so it is going to be white S it is going to be virtual by default you know from the previous tutorials and what we are going to do is that we are going to simply see out derived class function all right now in the main what we are going to do is that we are going to do upcasting actually and how we are going to do upcasting you already know you have to write in A then you have to define a pointer let's say it equal to A and then you have to write in new B so this means that this has been upcasted actually and then using this pointer A if you call in the function S and write in return 0 let's just run this code all right it has generated an error that says that class does not name a type yes it has to be in small c and now let's just run it yes it says derived class function so this means that the derived class function was called in even though the base class function was private now there are a few things to note about this program first is that this A write down here which we define in this function main function this is actually a pointer of the A type and it points to the B class object so when A arrow S is called the function of the derived class that is S it is actually called now the second thing is that int main is actually a friend of A class that is the base class so if you remove this friendship if you just comment it out it won't compile we will get a compile error now this behavior is totally different if you are dealing with Java and you know Java then you know that in Java private methods are final by default and they cannot be overwritten for example if we just run it now it is going to generate error and it is not going to allow us to make it a private function as you can see right here virtual white A S is private so we have to make int main a friend of the base class so if you don't use it it won't compile as you can see when I just remove the command statements it ran perfectly and it says it is a derived class function so I hope this is clear and you know now the answer to our question that virtual functions can be made private as well and in the derived class the public functions which was private here in the base class that was virtual white S it has been declared public in the derived class now and when you call in it using the upcasting the derived class method is going to be called in so I hope it's clear so we just covered an example so I guess that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Omar Khan now welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be covering about inline virtual functions so virtual functions in C++ are used to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of the derived class object so virtual functions are actually resolved at the runtime the main use of the virtual function is to achieve runtime polymorphism the inline functions are actually used to increase the efficiency of the code the code of the inline function get substituted at the point of an inline function call at compile time whenever the inline function is called so whenever a virtual function is called using the base class reference or pointer it cannot be inline but whenever it is called using the object without reference or pointer of that class it can be inline because the compiler knows the exact class of the object at the compile time so let me give you a quick example of an inline function and we will just move on to our compiler so right in here what we are going to do is that we are going to define two classes the first class is class A that is the base class and then we have another class B as usual that is derived class and it is derived from class A now in these two classes what we are going to do is that we are going to define a public function so what is that public function going to be it is going to be a simple function that is going to have a C out statement so we are going to write in virtual white s and what this function is going to do is it is going to simply see out this class and down here in this second class we are again going to have a public virtual function that has the return type of white and its name is s you don't have to specify this virtual over here it is going to be virtual by default so what we are going to do in this function is that we are going to have a simple C out statement that is going to simply print out derived class now the story comes completely into this main function where we are going to do our stuff now we are going to first define an object of the class A that is the base class and after defining that object what we have to do is that we have to define an object of class B as well that is B so next what we are going to do is that using A class what we are going to do is that we are going to create a pointer and that is going to equal to the object of this base class so it is going to equal to NB and this is actually a pointer of type A and it is pointing towards this B so now if we use this object that is right in here and we write in A dot S what I call in here then it is inline as S function is called through the object of the class but if you write in PTR and then write in this S function so basically what we are doing is that we are doing the upcasting thing in here and calling the S function so it cannot be inline as the virtual function is now called through a pointer so we will just put a return zero up here so if I just run this code you can see that first it printed out base class and this was the output that was from the inline function actually inline call of the function because that was called using the object of the class that was B dot S and then this output that said derived class it was actually the call that was made to a virtual function through a pointer now why because because in here what we have do is that we have created a pointer of the base class and we have made it point towards the object of the derived class which is actually upcasting so when we use this object that is the PTR that is pointing towards the reference of this derived class so when we are calling this function S down here this is actually not inlined as it is calling the virtual function of the derived class not of the derived class it is actually calling the virtual function through a pointer and this is actually completely inlined as we are normally calling a function S using the object of the class that is this and in this case the function S of the base class is going to be called in that says base class and this is going to call the function of the derived class so it is going to print out derived class for us as you can see right here on your output as well base class and then the derived class so this is what is the concept of inline function in C++ so I guess that's it with this tutorial thank you so much guys for watching and I will see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be covering about abstract classes and pure virtual functions so before we get on to the code part of this tutorial what we need to do is that we need to understand about abstract class and pure virtual functions separately so what are basically the characteristics of an abstract class now an abstract class cannot be instantiated but pointers and references of abstract classes type can be created abstract class can have normal functions and variables along with a pure virtual functions third abstract classes are mainly used for upcasting so that its derived classes can use its interface fourth classes inheriting an abstract class must implement all the pure virtual functions or else they will become abstract too now to talk about virtual function what is a pure virtual function basically so a virtual function is basically not used for performing any task it only serves as a placeholder when the function has no definition then such a function is known as a do nothing function the do nothing function is known basically as a pure virtual function a pure virtual function is a function declared in the base class that has no definition relative to the base class so a class containing the pure virtual function cannot be used to declare the objects of its own such classes are known as abstract base classes so the main objective of the base class is to provide the traits of the direct classes and to create the base pointer used for achieving the runtime polymorphism so now you have a basic idea of what an abstract class is and what a pure virtual function is so now let's move on to some examples so let's first solve an example of abstract class in c++ so I will move right in here to my compiler and right at the top what we are going to do is that we are going to create a base class and in this base class we are going to have a virtual function that is going to be a pure virtual function and it is going to be public so it is going to be virtual white let's name it as a and it is going to equal to zero now in the derived class what we are going to do is that is derived from the class that is base class so we are going to write in public base and it is going to have a function that is going to be public and it is going to be of the very same name as you know so it is going to be white a and it is going to simply see out implementing the virtual function in the derived class so quite obvious from this statement what is going on actually we are actually implementing the virtual function in this derived class that is named as derived and it is derived from this base class which has been declared right there at the top so now in the main function what we are going to do is we are going to first create an object of the base class let's name it as x and secondly we are going to create a pointer reference of the base class so let's name it as base b and then we are going to create an object of the derived class let's name it as y and then what we are going to do is that we are going to use this b to actually point towards this so we are going to write in b equals to the address of d and finally what we are going to do is that using this b what we are going to do is that we are going to call the a function so if you run this code now you can see that it will generate an error now why it has generated an error because this is basically an abstract class so since this is an abstract class so you cannot create an object of an abstract class since you are creating an object of an abstract class here so it is going to generate error and as you can see right here on your screens the error you can see that it says cannot declare a variable x to be of abstract type base so in this example basically the base class right here at the top is abstract which has a virtual function named as a which is equal to 0 and it cannot create object of itself so basically since this is an abstract class so you cannot create an object of itself simple enough so now the question arises that why we can create an object of an abstract class so basically when we create a pure virtual function in an abstract class we reserve a slot for a function in the v table but it doesn't put any address in that slot hence the v table is basically incomplete so as the v table for abstract class is incomplete hence the compiler will not let the creation of object for such class and it will display an error message whenever you try to do so so now let's move on to the pure virtual definitions basically so pure virtual functions can be given a small definition in the abstract class which you want at all the derived classes to have still you cannot create object of the abstract class also the pure virtual function must be defined outside the class definition so if you will define it inside the class definition the compiler will give an error so inline pure virtual function is basically illegal so now let me quote an example of how this is done so basically what we are going to do is that we are going to have this very same base class having a pure virtual function that is virtual void a you cannot give the definition of this white a right in here in this base class that is the abstract class because if you do so it will generate an error moreover you cannot also create an object as well similarly you will have this white derived class that is right here and it will be derived from the very same class and it will see out the very same thing that is implementing the virtual function derived class now the difference what it's going to be that between these means outside the base and the derived class both we are going to actually implement the function a now and what we are going to write in is that basically not here this white a basically belong to this class right in here so the function type is basically white it is derived from the base class and the name of the function is a so this is actually a pure virtual definition and this is the body in which the definition will be provided so if you write in pure virtual definition so three things to note about it first it is going to be outside the base class second it is going to be outside the derived class as well third it is going to be derived from this class that is the base class and the name of the function is going to be the very same and you can actually provide a definition of that function right in here so now if you move on to the main function there are going to be certain changes in it firstly you cannot create this object right in here secondly this base steric b is going to be the very same derived wife is going it is going to be the very same as well and then what you have to do is that it is going to equal to and d the very same way as you can see that base steric b and then it is equal to y and then if you call in this a function and just run this code you are going to not enough but yes this d need to be in small letter and now let's just run it all right now t it's going to be five and I guess the rest is perfect so you can see right here that it says implementing the virtual function in derived class so the main points which you need to understand from this are that first you cannot provide the definition in between the abstract class second you cannot create an object right here with the main function of the abstract class thirdly what you have to do is that you can also provide the definition of the function of an abstract class outside the abstract and the derived classes and I guess that's it so these were some of the main points and now you know basically that what is a pure virtual function basically so I guess that's it with this tutorial as well thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan now welcome you to another tutorial on virtual functions in C++ in this tutorial we are going to be covering a very interesting topic that is the pure virtual destructor so to work correctly classes with virtual methods must also have this function that is the virtual destructor and trustingly virtual destructors can be declared as pure which can be useful in many cases for example let's suppose that you have a base class which you want to make abstract now in this base class all the methods have meaningful default implementation and you want to allow the derived classes to inherit them as is however to make a class abstract at least one of its method must be made pure virtual which means the derived classes must override it so how do you make the class abstract in this case the answer is declare the destructor to be a pure virtual this will make your class abstract without forcing you to declare any other method as pure virtual so now let me just give you an example for what I've been speaking so far so let's move on to my editor so first we have to define a base class and then we have to derive a class that is named as derived and it is derived from basically the base class so now let's move on to what is going to be done here so first we are going to make a public destructor that is going to be virtual here so it is going to be virtual base and it is going to equal to zero which means that you cannot provide its implementation right in here and secondly you can define any method let's say virtual void and let's call it as as from the previous tutorial and you can also make it equal to zero if you want to all right so this means that the destructor and this function that is virtual void a cannot have its implementation right in here now if you talk about this function that is virtual void a so from your previous tutorial you know that you can provide its virtual definition but for this function that is the destructor function you must for this you can and if you want to you can provide its implementation that is the virtual definition basically but for this function that is the virtual destructor it is compulsory to provide its virtual definition so how it is going to be done you are going to write in the name of the class and then you are going to write in base and this body can be empty you can write in anything here you can just write in empty here but this definition this is compulsory so it is compulsory that you have to give the definition of a virtual destructor even if it is empty because if you don't give it i will just remove it and then run the code for you as well and then you will see yourself that it is going to generate error because this is something that is passed now for this function virtual void a it is completely your choice to provide its virtual definition or not in this case i'm not going to give its definition so right here in this drive class what we are going to do next is that we are going to actually define a pure virtual destructor here as well and here we can write in anything so basically as i told you before that when we declare the destructor to be a pure virtual this is how we are going to make a class abstract so this derived class basically here that is derived from the base class is now abstract as well which means that now you can also not create an object of this class as well so for example as in the previous case we were using our main function when we were creating basically a pointer and then we were creating an object of the drive class and then what we were doing was that we were actually referencing this y towards the object so now here just run it it is going to first generate an error here it was not supposed to yes this has to be made public so let's just provide the segmentation for this and now run it and you can see right here that it says cannot declare a variable object to be of abstract type derived not abstract this derived basically is also abstract as well so while defining a pure virtual functions it is very rarely useful that humans define a pure virtual destructor this is because the destructor of a base class is always called when a derived class is destroyed so failing to define it will cause a link error so now to just summarize what is what have been going on so far pure virtual functions are legal in c++ also pure virtual destructors must be defined which is against the pure virtual behavior the only difference between virtual and pure virtual destructor is that pure virtual destructor will make its base class abstract hence you cannot create object of that class there is no requirement of implementing pure virtual destructor in the derived class so i hope you've got the concept of this derived class and its importance and why we have to declare it so that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar han and welcome you to a new section here on this course on advanced c++ in this tutorial we are going to be starting on with a very interesting and very important topic that is templates so this is actually be the content we are going to be covering in this section we are going to start on with the introduction to templates then we are going to see the advantages of templates then we are going to see the types of templates that are used in c++ then we are going to see the parameters that can be passed to a template function and we are going to see the types of templates that are going to be passed into a template then we are going to see the features in templates then we are going to see the link list and templates and finally we are going to see specialization and its types to dog on with the introduction we are going to see the basic introduction of a template and we are going to define What is a template? And we are also going to see the advantages of using templates in C++. Now then in the section on types of templates, we are going to be studying the two types of templates that we have in C++. That is the function templates and the class templates. Moving ahead to the template parameters, we are going to see the four types of parameters that can be used in templates. The first one is type parameters, then we have the non-type parameters, then we have the template parameters, and then we have the default parameters. Then we are going to see the features in templates that includes template overloading. If you talk about link lists, then in that part, we are going to be seeing the relationship of link list with templates. And what are the advantages of using templates in linked lists? And then to the final topic that is about specialization, we are going to see the definition, advantages, and the types of specializations that can be used with templates in C++. So this is going to be a very important section and you are going to enjoy a lot. So that's it with this tutorial. In the next tutorial, we are going to be starting on with the basic introduction to templates. Thank you so much guys for watching up till now and I'll see you guys in the next tutorial. Hey guys, what's up? This is Amir Khan. Welcome you to the very first tutorial here on templates in C++. This is basically going to be an introduction tutorial in which we are going to see that what is a template and what is its purpose in C++. So starting on with the definition of templates. What is a template? C++ templates are a powerful mechanism for code reuse as they enable the programmer to write code that behaves the same for a data of any type. Now templates are among one of the most powerful features of C++ but they are often neglected and many of the programmers are unaware of the advantages of using templates. Now the question arises why templates? Now C++ requires us to declare variables, functions and most other kind of entities using specific types. However, a lot of code looks the same for different types. Especially if you implement algorithms such as quick sort or if you implement the behavior of data structures such as a linked list or a binary tree for different types, the code looks the same despite the type used. Now if your programming language does not suppose a specific language feature for this, you only have bad alternatives. Now what are those bad alternatives? The first alternative is that you can implement the same behavior again and again for each type that needs that behavior. Second alternative is that you can write a journal code for a common type such as object or avoid. Third is that you can use special preprocessors. If you come from C, Java or similar language you probably have done some or all of this before. However, each of these approaches has its drawbacks. First one that if you implement a behavior again and again, you reinvent the wheel. You make the same mistakes and you tend to avoid complicated but better algorithms because they lead to even more mistakes. Secondly, if you write journal code for a common base class, you lose the benefit of type checking. In addition, classes may be required to be derived from special base classes which makes it more difficult to maintain your code. Thirdly, if you use a special preprocessor such as a C or C++ preprocessor, you lose the advantage of formatted source code.  Now templates are a solution to all of these problems without these drawbacks. They are functions or classes that are written for one or more types not yet specified. When you use a template, you pass the types as arguments explicitly or implicitly because templates are language features. You have full support of type checking and scope. In today's programs, templates are used a lot. For example, inside the C++ standard library, almost all the code is template code. The library provides sort algorithm to sort objects and values of a specified type, data structure to manage elements of a specified type, strings for which the type of a corrector is parameterized, and so on. However, this is only the beginning. Templates also allow us to parameterize behavior, to optimize code, and to parameterize information. All this stuff is covered in the future tutorials. We'll start on with some easy stuff and then move off slowly. So in the next tutorial, we will discuss the types of templates. So that's it with this tutorial. Thank you so much guys for watching, and I'll see you guys in the next tutorial. Hey guys, what's up? This is Sumer Khan. Now welcome you to another tutorial here on this section on templates. Now in this tutorial, we are going to be seeing the types of templates in C++. Now the study of templates go in two directions. The first one is function templates, and then we have class templates. So in this tutorial, we are going to be discussing both of these in detail while examples. So starting on with function templates. A function template works in a similar way to a normal function with one key difference. A single function template can work with different data types at once, but a single normal function can only work with one set of data types. Normally, if you need to perform identical operation on two or more types of data, you use function overloading to create two functions with the required function declaration. However, a better approach would be to use function templates because you can perform the same task without writing much more code and that can be done by writing less and maintainable code. So this is actually the template function template. Hey guys, what's up? This is Sumer Khan and I welcome you to another tutorial here on this section on templates. In this tutorial, we are going to be seeing the types of templates in C++. Now the study of templates go in two directions. The first one is function templates and the second one is class templates and in this tutorial, we are going to be seeing both of these in detail with a lot of examples. So let's start on with function templates. Now a function templates work in a similar way to a normal function with one key difference. A single function template can work with different data types at once, but a single normal function can only work with one set of data types. Normally, if you need to perform identical operation on two or more types of data, you use function overloading to create two functions with the required function declaration. However, a better approach would be to use function templates because you can perform the same task by writing less and maintainable code. Now a function template start with the keyword template followed by a template parameter insight which is followed by the function declaration as you can see right here in the slides. In the slides here, this T over here, this T over here is the template argument that accept different data types that can be either integers, floats and anything else. And this class is a keyword. You can also use the keyword type name instead of class in right in here in this syntax. So when an argument of a data type is passed to some function, that is right here, what it do is that the compiler generates a new version of the sum function for the given data type. So now let's move on to our compiler and solve some examples so that any ambiguity you have is removed. So here we are at our compiler. So right in here we are going to solve a very interesting example. We are going to initialize in the main function to integer values, to float values, and then to character values. And then using a simple only a single template function, what we are going to do is that using that very single template function, we are going to check if what is greater between the two integer, what is greater between the two float values, what is greater than the two character values. As you know, that if you're dealing with normal functions, and you are doing with something like functional overloading, you know that functional overloading is basically in which you actually vary the number of arguments, or you just change the type of values that are going to be passed into that function. For example, if you have two functions, if you're talking about functional overloading, then in one you have, let's say two arguments that are integer and a float, then in the second one, you have both the float arguments. So this is actually basically code reusability. If you are doing the same job, for example, you are checking if what is greater between between the two integers and what is greater between the two floats. For example, what I'm talking is that for example, you write in white, the function name is greater, and then you receive it in i and in j. And what you do is that you just return i is greater than j. So for example, you have this very same function, just copy it. And what you do is that you only change the type of parameters, both from integers to both to floats. So this is actually the concept of functional overloading. Now, as you can see that both are performing the same job. For example, if I just simplify it much more because this is a white function, so it should return nothing. So I'm going to write an int add equals to i plus j. And then write in here as well that int add equals to i plus j. So you can see right here that both these functions are actually performing the very same task. But since the type of parameters that are passed in in this, we are passing in the both integer values and then this we are passing in both the float values. So since the type of the argument is different, so you cannot pass them into the same function. For example, if you call this function right above inhale, you have to write in greater. And then you have to pass in two values that are integers. And then to call the second one, what you have to do is that you have to pass in values that are both floats. So if you pass in something to this that is float, so what's going to do is it's going to simply generate errors because this is what it's not going to accept. So this is the case where the job of templates come into work and it is very handy to use templates right in here. So what templates do basically is that using templates, for example, let's just initialize two variables in i and j, then initialize two float values, let's name them as x and y, and then initialize two character values as a and b. So now we have to integer values to float values and two character values. What we are going to do with them is that we are going to see out enter two integers. Alright, then what we are going to do is that we are going to simply see in i and then we are going to see in j. Then what we are going to do with both of these is that we are going to see out. Let's just do it that later after declaring that play let's just first make the see out statements. Then in second case what we are going to write in that enter two floats and we are going to write in c in x and y. Then we need one more see out statement here and then we are going to see out enter two characters and we are going to see in a and b. Good enough and then we need one more see out statement here. So now let's just define our template right up here. So if you see this case then by the concept of function overloading you must be thinking that you have to initialize a separate function for this a separate function for this and a separate function for this but in case of templates what we are going to do is that we are only going to need a single template function for all of these. So how it is going to be done it is going to be done right in here. So the building name template then we have to use the keyword that was class then we have to write in this t over here that is actually name of the template. Then what we are going to do is that we are going to return n1 is greater than n2 fn1 else n2. Alright so now what we are going to do is that we have defined the template right in here. Now what we need to do using this t here is that we are going to define a function this is actually a template. So now we using this t right in here that is actually if you can consider this as the instance of this class so using that instance what we are going to do is that we are going to define a function use that t then n1 then t then n2. Now we just scroll down here then I'll explain what I've done up there. So what we are going to write and we are going to write in large and we are going to pass in these two values that are i and j and we are going to write in is larger and we are going to enclose it in quotation marks. Alright good enough. So let's just copy this paste it right down here change these values x and y again paste it right in here change these values to a and b. Now as you can see that I have called a very same function that is initialized only once but I am passing first integers to that function then I'm passing floats to that function and then I am passing characters to that function that function is right in here. Now what is templates actually as I explained you here that templates is going to work for different types of data arguments now as you can see that for the first time this function is going to receive an integer values. So this function that is named as large that is initialized using the instance of this class named st it does not care what is sent to me it is going to handle each and every data type so for the first case it is going to deal with integers. Now after returning its output printing it out on the console screen the second time this function is going to be called in for float values so it does not care that previously I was called for integer values so why I am being called with float values now it does not care at all it is going to deal the float values as well and return our output finally it is going to be called in with character values it again does not care that I was previously dealing with integer values then with float values it does not care at all it is going to deal with the character values as well so you can see right in here if we were using function overloading we would have initialized three functions one for receiving the two integer values one for receiving both the float values and one for receiving the character values and all this function would have been doing the very same job that was to return the value that was greater so you can see that the job of three function has been done using a simple and a single template function only so how you can use code reusability as I told you in the advantages of templates that templates is actually going to reduce of a code it is going to make the code much more maintainable and what is going to do is that it's going to reuse of a code so as you can see that this set of code is used three times first for integer then for floats and then for character values so if I just run this code now all right it says enter two integer values so I'm going to let's say enter two and press enter and then press three so it says that three is larger then I'm going to enter two float values let them be four point three and three point four let's say oh let's make it equal to five point four four point three it says five point three is larger so now I'm going to enter in characters so characters let's say I entered a and then I entered b so it says b is larger so you noticed that how this simple template function handled all the three queries so the code was reused and everything worked so perfectly so now let me just show you one more example of a simple template function so I will do what is that I will remove this stuff from here and now in this template function right in here we have this template and as I told you before that instead of this class what we can use is that we can also write in type name so you can write in that class it's going to work perfectly for you but also you can write in this type name up here so as I'm telling that you can also do something like class and t so you can use any of these these are the two options available with you so for this example I'm going to be using type name t since for the previous one I was using class t so to show you that how this work that's why I'm using it so now using this t what we are going to do is that we are going to define a function so let's just remove this from here so we have template type name t we are going to write an int compare so in this template function what we are going to do is that we are going to compare two values so we are going to write in constant t and value one then constant t and value two so these are going to be the two values we are going to be comparing so then we are going to write in if value at location one is less than value at location two then it's going to return minus one else if value at location two is less than value at location one then what it's going to do is that it's going to return positive one so now in this main function let's just add some space what we are going to do is that we are going to see out call in compare and in this compare function what we are going to write in we are going to specify the type then we are going to write in 10 and 20 then and l we can also write in see out compare then we can write in let's say string this time and we can pass in some string like let's say h and w and we can also use in something like double values so in this case we are also comparing first integers then we are comparing string values and then we are comparing double values so in the double values let them to be 30.5 and 20 point let's say do good enough let's just remove these spaces from here all right so this is now much more visible so for the first case it's going to be calling the function named compare right in here with integer values then with string values and then with double values so let's just run it all right it says that h was not declared in the scope yes it was not because it needs to be in quotation marks w also needs to be in quotation marks so now let's just run it i guess they need to be put into double quotation marks now let's see if it works yep that works now so it says minus one minus one and one because for the first two that were this 10 and 20 and then this h and w you can see right here that this 10 is smaller than 20 which means value one value two is greater than value one and then this case also that w this condition right at the top is true because w is greater than h so for the first two for integer and string this first condition was true so it returned minus one and then for the second one you can see that this 30.1 is greater than 20.2 so the second condition right in here was true and it returned one so this doesn't matter what does it return all that matter is the efficiency of this code you can see how this small code right in here a sim a single template function is dealing with three types of values for integers string and double values so i guess it is clear in the future tutorials we'll also be covering more detail about function templates in this tutorial you only need to know about the basic introduction and syntax of the function templates and how the advantages are actually and practically implemented so now let's move on to the second type of template we use in c++ that is right in here class templates similar to function classes can also be parameterized with one or more types container classes which are used to manage elements of a certain type are a typical example of this feature by using class templates you can implement such container classes while the element type is still open like function templates you can also create class templates for generic class operations sometimes you need a class implementation that is same for all classes only the data types used are different normally you would need to create a different class for each data type or create a different member variables and function within a single class this will unnecessarily bloat your code base and will be hard to maintain as a change in one class or function should be performed on all classes and function however class templates make it easy to reuse the same code for all the data types now the question arises how to declare a class template now this is actually the syntax of using a class template that is right up on your slides in this declaration this t is the template argument as in the previous case right in here and it is also a placeholder for the data type use inside the class body a member variable var that is right in here and a member function named as some operation that is also used and with this t right in here is going to be used and they are both of type t now before moving on to the example you need to see one more thing right in here and how to create a class template object you can see its syntax right on your screen to class create a class template object you need to define the data type inside a greater than and less than one creation you can see right in here this is going to be the data type right in here for example it can be something like integer something like float and something like string as it's also showed you in the example we just solved in here right here as you can see right in here we have this integer we have this string and we have this double in case of class the only difference it's going to make is that it's going to be something like first we are going to have the class name then in this what we are going to do is that we are going to actually define the data type and for example for the first time it is integer then finally we are going to write in the class object here so to just copy this stuff from here paste it right here paste it right here and all you need to do now is just change something like in here make it float and then make this final one a string so just a basic difference in this you have to first write in the name of the function but in here you have to write the class name then integer string a double is going to be the very same and then you specify in values but in here what you are going to do is you are going to write in the class object so let's just remove it so this is actually the simple syntax for doing so so finally now let's move on to some example to see how to create one of your own class templates so let's again move to def c++ and let's just remove this stuff from here also let's remove this template from here so in this example we are going to start right on class template we are going to be studying a very interesting example we are going to be creating a simple calculator function in which we are going to be returning the additions traction product division and these four basic operations we are going to be only dealing with we're not getting to get into much more complexity because the purpose of this tutorial is only to get you a basic concept about class templates so what you need to do right up here is that you have to write the building word template then in these brackets what you're going to write in you are going to write in class t which means that this t is going to be the instance and this is going to be the name so this is actually a keyword you have to always use you have to use this class or you have to use type name so in this case we are going to using this class so now you have to define a class that is going to name a scalulator and these are the brackets for that so the first thing we need to do is that we need to define in the variables because the first thing you need to do is that you have to write in num1 and 2 now these num1 and these num2 are going to be the values we passed into this class and these values are going to be initialized using the constructor function now these num1 and these num2 can be of any type because we have written the data type here to be this t which is right in here so these num1 and num2 can be initialized to be some values for example integers loads or anything else so we have these two values then we will have a constructor that is going to be public so it's going to be named as calculator and this constructor is going to receive in two values the first one is going to be let's say a and the second one is let's say going to be b this has to be calculator i guess that spells correctly now yes it does so you can see right in here that the data types of whatever values are passed into this constructor function are going to be t which means that any data type values can be passed into this function and what it's going to do afterwards that is going to make num1 equals to a and num2 equals to b now since these are also the values that have a data type of t and this a and b also have a data type of this t right in here so whatever value whatever data type is received in here can be stored in this num1 and num2 because they are also of the type t so after that what we need to do is that we need to make a function that is going to display our outputs so let's name it as display and in this function we are going to simple c out first we are going to see how the values so we are going to write in the values r first we are going to write num1 and num2 and l so these are going to simply see out of values after that what we are going to do is that we are going to perform the addition so we are going to write in addition is so we are going to call in the add function and we are going to just be making add functions as well then after that we are going to be printing the subtraction of the two numbers subtraction is time it's going to be subtract then we are going to see out the product of the two numbers so we are going to write in product is multiply and finally we are going to be printing the division so we are going to write in division is divide and l so good enough so this is our display function the display function is first going to simply see out the two values that are the number one and number two then what it's going to do is going to perform the addition using this add function and just in a moment we are going to be initializing this function as well then it's going to print the subtraction using the subtract function which we are also going to initialize then this product using the multiply and then divide division using the divide function respectively so right down here what we are going to do is that we are going to initialize our function so the first function is the add function as you can see right in here that the return type or data type for that function is going to be t which means that this function is actually going to return something so what it's going to return is that it's going to return num one plus num two so it's going to return num one and num two which means that it's going to have a return type as well so it cannot be void so what it's going to be it's going to be t because if you write in here int and the returned value of num one plus num two is a float then what it's going to do is going to generate an error so t is a general data type as you know now so it's going to return any kind of data value it may be characters it's going to be a float value it's going to be double value it's going to be integer it does not care so the second function we are going to use right here is going to be the subtract function and its return type is also integer and this is going to be the body of that and what it's going to do is get to return num one minus num two then we will have this third function whose return type is again going to be t and it's going to be multiplication function and what it's going to return it's going to return num one's direct num two which means it's going to print our product and then finally we will have a divide function with a return type of t as well and what it's going to do is that it's going to return num one divided by num two so good enough so right in here are our class hands so let's move to code of our main function now right in here so in the main function what we are going to do is that we are going to first initialize an object so we are going to write in calculator and let's say we want to first initialize some integer values and then we are going to let's say use it for float value as well so we are going to write in float and we are going to write in float calculate this needs to be in capitals this c has to be in capitals as well and we are going to pass in let's say 2.3 and let's say 2.4 now finally what we are going to see out is that we are going to see out integer value results and how they are going to be printed is that we are going to write in end of way dot display similarly when you have to write in the results for the second one let's just add few spaces first and then we are going to write in float value results and we are going to be using the second object that is the float calculate dot display and finally return zero so let's just run it all right it says that yep the return value cannot have an endel silly mistake so now let's just run it so this is the word right in here it says the integer value results the values are 2 and 1 the addition of these two number is 3 as you can see plus 1 equals to 3 subtraction is 2 minus 1 equals to 1 the product is 2 into 1 equals to 2 the division is 2 divided by 1 equals to 2 then we have the float value results right in here the values are 2.3 and 2.4 the addition is 4.7 the subtraction is minus 0.1 the product is 5.52 and the division is 0.958 triple 3 so you can see that using two instances of a class right down here you can see that you have the class name then you have to specify the type and then you have to rise in the object and then using that object what you are going to do is that you are also going to call the constructor function as well so the constructor function 2 and 1 were passed in for the first time for the second time 2.3 and 2.4 were passed in so you can see that how using this t right in here does our job for integer values as well as for float values using a single class so that's how actually templates are going to work for us you can you see in the previous example that how function templates simplified of a job and reduced of a code and now in the case of class templates you see that how a single class name calculator was able to calculate the values for both integer values as well as float values and hit for able to generate values for us as well as you can see right here that the functions can have the return type also of any type so I guess that's it with this tutorial so now I hope that you know that what is a template what is a class template and what is a function template and what is its use in C++ and how they are useful other than function overloading and other alternatives I told you so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Samar Khan now welcome you to another tutorial here on templates in C++ in this tutorial we are going to be studying about template arguments a template definition starts with the keyword template followed by a list of template parameters what follows after that is either a class definition or a function definition defining a class template or a function template respectively the template parameter introduces names into the scope of the definition which can be types values or templates these names can be used just like any other name of the same kind then when the template is instantiated real types values or templates are substituted in place of these names and the code is compiled now in C++ we have these types of templates templates can have one or more template parameters that can be type parameters non-type parameters template parameters and default parameters two template instantiations refer to the same template if their parameters are all the same irrespective of any type definition that may apply so if you consider this example right here on your slides in this vector one vector two and vector three are all of the same type now why they are all of the same type is a simple explanations that when the instantiations refer to the same templates and the parameters are all the same then irrespective of any type definition that may apply the vectors are always the same in this case all of these refer to the very same template so all of these vector instantiations are the very same and these three vectors are also equal so moving ahead to multiple parameters now multiple parameters may be specified that can be separated by commas in the template parameter list you can see the syntax right here on your screen template is the built-in word then we have type name t1 type name t2 you can also write in class t1 and class t2 and then we have class my class now my class in here is thus a class template with two template type parameters that are t1 and t2 every time a template is referenced with distinct template arguments then the template is instantiated with the argument substituted as explained in the following sections so if the resultant code is not valid then a compilation occur error may occur so now let's start on with the template type parameters template type parameters are template parameters that refer to a type they're the most common form of template parameters their syntax is very simple as shown right here on your screens template type we have first type name and we have the name or you can also write it as class name as i told you that you can write in type name as well as class and then you have to write in the name now both alternatives are identical in meaning and the choice is nearby a matter of style name is any valid c++ symbol name right in here once name has been introduced in the template parameter list then any reference to name within the body of the template automatically refers to the type of the corresponding template argument for each instantiation and can be used anywhere a type can normally be used so let me just give you a very quick example so let's move on to our compiler right as here is our compiler with the main function already there so at the top right in here what we are going to do is that we are going to write in template this is actually the syntax for doing so so we are going to then write in type name t we can also write in class t as you know then you can write in void function and what it's going to do is that it's going to have t value that is actually the argument so in this case if we have let's say a constant reference that is equal to the value that was passed into this function and we create a pointer to this instance that is the new t and then when we write in t temp 23 now if you write something the function for the code generated for the function int is then going to be the very same as if you write in function int int value and you initialize a constant integer reference that is equal to value and you write in int star like p equals to neo int and then int temp 23 so if however our reference was made to the function that was for strings then a compilation would result as the statement standard string temp 23 will not be valid so these two codes right above that is the void func and this is also void func but in this case it was integer and in this case it was for any kind of value any kind of data type values so these two in at this time right in here are the very same for integers for sure but if you write in this function and pass in some string value to this function then for sure it's going to generate error because it is not for functions it is for integers and any other kind of data values but this is only for integers so this is for a template type parameters this is how template type parameters actually look like so moving ahead so the next one is the template non-type parameters the non-type template parameters are template parameters that are values rather than types they can be any value that is a compile time constant their syntax something look like this right here on your screen that is template then in this variable can be anything any variable you want to declare right in here so for example let's just code it so right in here let's just remove this stuff from here and we are going to write in template this is the parameter as I told you and then we are going to initialize any variable let's say int i is variable then for example if we have a class a then we have a template let's say it's time it's going to be a double value tb and then we have a class b then we have template white function name style and then let's say we have a white function name let's see so the class template a has actually a non-type template parameter which is an integer the class template has a non-type template parameter which is a pointer to double and function template c has a non-type template parameter which is a pointer to a returning function white with a single integer parameter now if you want to use it then some of the example of its uses can be stated as let's say right here and the main function as you can write in for example if you want to use let's say the class a and for that you are going to write in a then you have to pass in the value and then a3 that is the name of the vector in this case then you can just write in a size of and then you can write in string and you can also use it as something double d that is at global scope then you can use the class d is class b sorry as well that can be class b and you can write in and d in here you can also use it as something and you have to also specify the name of the object here so you can write in bbd here and you can also write in something like null value in here and then you can also write in the end here so you can also use the function that is the last function as my function and the integer argument and then you can also use it in struct my class and in this struct my class what you can do is that you can write in static white static function space right up this don't need to specify it here so this is our actual class and then in the main function these all need to be outside the main function right here in the main function what you can do is that you can write in in c my function and you can write also as c and my class and then the name of the function you have specified right here that is the static function and then this column over here so within the definition of the template actually the name of the non-type template parameter refers to a constant of the appropriate type so if a given you can for example if you have this function right at the top here you already have a template int i then in the function what you can do is that first let's just remove this stuff from here then for example you have a function then what you can do right in here is that you can simply see out and then if you just move into the main function down here and just print in funk tree and just run this code it's not f u and c it's f y i f y and c i just misspelled it so you can see right here that tree is all going to be over output because the vector that was passed in was passed into this integer value and it just simply printed out so you can see that this template actually received the value of integer for example you for passing any value for example 999 run this code you can see that it will print 999 for you right here so for example if you pass in some let's say float value and you can see right in here that it generated error for us because this template is a non-type template parameter and it is going to only receive integer values so this cannot have float values or any kind of character values so at compile time constant is something that can be evaluated at compile time this is what is at compile time so a compile time constant is something that can be evaluated at the compile time such as an integer or the address of a global variable these are these are the actually restrictions on what can be a compile time constant for example float floating point values cannot be compile time constants so the next type of parameters we have are the template template parameters template parameters enable a template to be parameterized by the name of another template say for example that you have a class which contain a couple of collection of items some strings some integers and you want the user of your class to choose what type of collections to use vectors list stack etc now the natural thought is to make the collection type a template parameter however a collection of string is a actually different type from a collection of integers so the user will have to specify both individually if template type parameters are used now the solution to this template template type parameter looks something like this because this is the syntax for that that is template you have to again write in template and then the type name t r class t you can use any of that so to give an example of this let's move on to editor let's just remove this stuff from here so right above at the top here for example we have a template then again template and then we have type name t so for example now that we have a class named as container type and then we have class my class and this what we have actually is that we will have container type integer value and we will have an integer right in here integer container and then what we are going to do is that we are going to write in container type and we are going to pass in string then string container that is for the rest of the club now container type is a template template parameter that refers to a template with a single template type parameter you can just say that my class right in here with a vector that is right in here that compile container type and then we have this vector right in here or you can say that my class or some list to have vectors or lists respectively now within the template definition the template type parameter can be used just like any other template you want to use in so this is a very simple concept of template type parameters and this is how its sync test look like now then we have a default uh parameters of templates that are also very simple as well can in this just as function can have default values for their arguments so again templates indeed this facility works in pretty much the very same way if a template parameter has a default specified then all the subsequent template parameters must also have a default specified now when referencing a template parameters with default values can be omitted if a template parameter is omitted all the subsequent template parameters must also be omitted so that's uh it with the template type parameters template non-type parameters template template parameters and then template default parameters so these are the four types of parameters we have to use in templates in C++. So I guess that's it with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umar Khan and I welcome you to another tutorial here on this section on templates. In this tutorial we are going to be starting on with a very interesting topic of templates that is overloading function templates. Now you may overload a function template either by a non-template function or by another function template. Now if you call the name of an overloaded function template the compiler will try to deduce its template arguments and check its explicitly declared template arguments. If successful it will instantiate a function template specialization then add this specialization to the set of candidate function used in the overload function resolution. The compiler proceeds with overload resolution choosing the most appropriate function from the set of candidate functions. Non-template functions take precedence over the template functions. Now if you talk about some of the examples of how these preferences are taken and how a template function choose to call itself or to call some normal function. So for that let's solve a very simple example of how this is all done and then I'll explain how it's done. So let's move on to our editor right in here and we will define a template and let's say we make this as t then down here we will have a function of that that is going to be named as f and we are going to receive in two variables tx and ty. Now as you can see right in here that these two variables can be of any data type since they are specified here as of data type t. So now in the body what we are going to do is that we are going to simply have a statement that say template and align space. Now what we are going to do after that is that we are going to define a normal function. So the function is going to be void f then it's going to have two arguments let's say in w and in let's make it f and it's going to see out non-template and now in the main function what we are going to do is that we are going to actually call the functions and then the calling is going to decide that which function is going to be called rather it is going to be this template function or this non-template function. So we are going to write in f as you can see right in here both have the name f so the argument is going to decide that whatever we pass into these is going to decide that which of these is going to be called.  So if I just run this code and show you the output so you can see right here that it says non-template for the first one that is f 1 comma 2 then it says template for the second one that is a b and then it says non-template for this third one. So the function called f 1 a 2 could match the argument types of both the template function and the non-template function. The non-template function is called because a non-template function takes precedence in overload resolution. I repeat it again the non-template function takes precedence in the overload resolution that's why we have this non-template right in here. If we talk about the second one that is f and then we have passed in two string arguments then this function call can only match the argument types of the template function. So that's why we have this template over here because as you can see that both of these are characters and in this normal function we are receiving both as integer values. So it's clearly failing this concept so it is going to call and this template function and this template is going to be printed out. Now for the third one it is a very interesting gates. Argument detection fails for the function called f 1 and this b right in here. The compiler it does not generate any template function specialization and overload resolution does not take place as well. The non-template function resolves this function call after using the standard conversion from character to int for the function argument b and then print out this no