 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. Code is replaced by some stupid text replacement mechanism that has no idea of scope and types. 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 you write in 1 comma 2 then we write in two variables a b and finally if you write in 1 and then we write in let's say b. 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 non-template. So you can see that how non-template template and non-template was printed out in this sequence. So let's solve one more example. So we will just remove this stuff from up till here and then we have this template class t. Now down here what we will do is that we will define a function let's name it as x and what we are going to do is that we are going to have arguments let's say the argument name is x or not x let's name it as y since the name of the function is x. So as you can see right in here it has to be t not a so you can see right in here that it is going to receive a value that is of any data type and it is going to store it in y. Now in the body what we are going to do is that we are going to see out template variable is y in this case. All right so now the next function that is going to be an overloaded function using the name x and what we are going to do is that we are going to write an int f which means that it is going to receive an integer value and then we are going to write in integer value is f right in here and then this and l call in at the end so I will just remove this from here and what we will write in that we will call this x and let's pass some stuff like let's say call m from here and then let's call this x by passing pure integer value and then call this x by calling a float value so these are all the possible combinations in which this function x can be called in and finally return zero so if I just run this code now all right to our extra now let's just run it yes this needs to be in small this also needs to be in small this is going to be small x not a capital x so let's just run it now and it ran perfectly so it says the template variable is m integer value is 50 the template variable is 40.6 so now in this program actually the function x is overloaded one version contained template arguments and the other version contained integer variables in main the x function is invoked tries with character values at the first time then with integer values and then with float values the first call executes the template version of the function x the second call executes the integer version of the function x and the third call again invokes the template version of the function x so why does it do it so is the very same logic as you can see right in here when this m was passed in this over here that is expecting an integer f to be received it failed completely this is a past so this is actually printed out so the template variable is and this m is going to be printed out since this m was received in this variable y since this has to be any data type then when this x 50 was called in this int f or you may say that this function over here this overloaded function is going to receive it because as I told you before that the non template actually takes precedence over the template once so this is going to be called and it says that the variable integer value is actually 50 and then when 40.6 was called in it was a float value so this failed completely this was called in and it says the template variable is 40.6 so this is actually how you can actually overload a function template so you have already understood what are the preferences and we have already covered the examples 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 Umar Khan and I welcome you to another tutorial here on this section on templates in C++ in this tutorial we are going to be seeing templates by abstracting our types specifically we are going to see that how you can use templates while coding your data structure for example linked list now a good way to write template is to start on with data structure or function that works for a particular type and then generalizing that type to a template parameter now you guys know how to code linked list already in the previous section now as you know that when you initialize a linked list every data type has to be offset in a for example if you have a linked list that contain integer values then all the nodes that are going to be a part of that linked list are going to store integer values and if you want to store for example a float value you have to make a new structure and then you have to store float values in that similarly if you want to store character values then you have to make another linked list in which the structure has to be made a new one and in that you have to initialize character so now what if you want to store float values integer values and character values into a same data structure or a linked list then you know templates is going to help you out how templates is going to help you out let's see with the help of an example so let's move on to our editor so here we are now the first thing you need to do is that you have to define a structure now a normal structure something looks like this for example if you have a structure named as link then what it's going to have that is going to have for example it's going to store integer values so the data part is going to be integer and then you have a pointer that is the next pointer and that is initialized using the name of the structure now what if you want to convert this simple structure to a template structure so that it store any kind of data values so for that what you have to do is that you have to write in template type name t and the changes that are going to be made in this struct link are that the data is going to be of any data type which means that you can now send in any kind of data to this structure so this is a very simple change you have to make so that it can now store any kind of data values now there are certain more changes now what are those changes now as you know that when you initialize a class for example the name of the class is linked list now the change at the top of this is going to be you have to also make this a part of a template that is named as t so now in this class what we are going to do is that we are going to have private members and in the private members what we are going to do is that we are going to write in link then we have to write in this t which means that this is actually the scope of this link list at this time and then we are creating a pointer named as first and this is actually the pointer that is going to point towards the very first link then we are going to have public items as well so we are going to write in public and then the public as you know is going to be the constructor function and in this constructor function what we are going to do is that we are going to initialize first equal to null simple enough now outside this public what we are going to do is that we are going to have two functions the first one is going to add item function that is going to actually add data items one link at a time and it's going to have a data type of t which means that it is going to receive value this is actually the function initialization part we are actually initializing the function named as add item and what is this function is going to do is it is going to receive values of any data type that's why we have specified t right in over here then we will have a display function as well now down here what we are going to do is after this class is that we are going to make the two function the first one was add item and the argument received was td and this is the body for that now some more changes that need to be made made above this are that we have to initialize template class t here as well and when you use this name you have to always use this t with this so that this t actually defines the scope of this link list so now we can easily actually use of a body and in the body what we are going to do is that we are going to write in link t right in here not outside steric new link equals to new link t which means that a new node is going to be created and in the new node what we are going to do is that we are going to write in new link since this is the pointer through which the new node has been created so in this in its data part what we are going to do is that we are going to store this d right in here which is received at the time of when we call this function add item and this d now can be of any data type since we have specified this t right in over here so always remember when you have to use the word link list when you have to use the word link you'll always have to add this t so that this t define actually the scope of this link list so now we have added the data part now what we need to do is that we need to insert add head of the link list so we are going to write in new link next is going to equal to first and then first equals to new link not a space between them good enough now what we need to do is that we have to make our display function right down here so we are going to write in template glass t and right after this we are going to write in white link list and a t right in here and then the name of the function display is going to be the name of the function and in the body of this function what we are going to do is that we are actually going to make an object and the name of the object is going to be current equals to first which means that we have to actually print the list so we are going to start on from the very first node since this is the very first node so we have actually made this pointer nameless current and pointing pointed it towards this whatever this node is pointing at so since this is pointing at the first node so this means that it's current at this time is also pointing at the very first node so now what we want to do is that we want to iterate this from the very start till the very end and print each and every element on its way so we're going to write in while current is not equal to null what it's going to do is that it's going to simply see out the data at every node and after printing the data at the node what it's going to do is it is going to move one step ahead using current equals to current next now to code the main function first thing we need to do is that we have to make the linked list object so we are going to write in linked list let's li be the object then what we have to do is that we have to add elements to that linked list so we are going to write in add item so for first cases since we have made this as int so what we are going to do is that we can only pass in integer values so for example we pass in two then we will just copy this from here and let's just pass in four up here six to this and then eight to this now for example we have passed in these four values now what we want is that we also want to let's say pass let's say corrector values so before passing let's just first call this play function for the integer values now we have to make another object linked list and in that instead of t what we are going to write in what we are going to write in character which means that now we can pass in actually character values so let's name it as lc this time and then using this lc object what we are going to do is that we are going to call in this add item function place this column here this arrow here place this column here let's just copy this again paste it here paste it here paste it here and let's just make it equal to b c and d and then we have to call in the display for this function as well and for example then you have to also pass in let's say float values or let's say long values or any kind of other values for example if I just write in float and let's just make it to lf this time and we are going to write in lf dot this is the function let's say 4.3 was passed in then we will just copy this and three values pasted let's make it equal to let's say 4.9 let's make it equal to something well 4.4 and finally let's make it equal to 7.9 and we have to also call the display function using this object so it's going to be lf dot displayed and finally we are going to write in return 0 so now if I just run this code here you can see that we have 8 642 at the top the integer values then we have dcba then we have 7.9 5.4 4.9 4.3 now you as you can see that the order has been scrambled a bit it's 2468 and then a vcd and 4.3 4.9 you can just make it equal to ascending order you know how to make it from your tutorial on linked list I guess I made some problem here so that's why it's printing in the decreasing order so the purpose of this tutorial is to just tell you that how using a single linked list that is named as link you can pass in float values you can pass in corrector values and you can pass in integer values as well since from the knowledge of linked list you know that only a single data data type elements can be stored in a single linked list but now using templates what you have done is that you have modified that concept and now you can store any data type value in a single linked list but by using templates so I guess that's it because in this tutorial you are to understand the basic example of this linked list and its association with templates 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 templates in C++ in this tutorial we are going to be covering the topic template specialization and we will see what is the special behavior that can be exhibited by templates now templates in C++ is a feature we write code once and use it for any data type including user defined data types for example if you have a sort function then that sort function can be written and used to sort any data type of items a class stack can be created that can be used as a stack of any data type now what if we want a different code for a particular data type consider a very big project that needs a function sort for a race of many different data types so let quick sort be used for all the data type except character values in case of character values total possible values are 256 and counting sort may be a better option so is it possible to use a different code only when the sort is called for the character data type yes it is possible in C++ to get such a special behavior for a particular data type and this is what is called template specialization so let us see some example of how template specialization is different from the normal functions and how you can use template specialization for achieving a special behavior for a particular data type so let's move on to a compiler here we are now as a totally above that we have we are going to have a sort function and for every other data type it is going to be a simple template function but for character values it is going to have a special behavior so for example this is our template and we have class T right in here then we have this sort function and in this sort function we receive an array which we have to sort actually and then this is the size and finally what we are going to do here is that we are going to write the code that is to implement the quick sort so this is a basically a generic sort function now what if we want to have a template specialization specialization function that is specialized for character data types then how it is going to be coded and how it is going to be different from this one at the top here we are going to write in templates as usual then what we are going to do is that we are going to have these two brackets and we are going to specify nothing in between and this is what is the syntax of template specialization as well so right down here what we are going to do is that we are going to write in white sort and then we are going to write in the value that data type values for which we are going to make this template specialization function so in this case we are going to do it for character values so we have to specify character over in here and then in this case we were expecting any kind of data type but now what we are receiving is that we are receiving a character array and then the very similar in size and what we are going to do in this function is that we are then going to write the code to implement counting sort so this is basically the template function that is going to have the template specialization a function specialized for character data types only so another example could be let's say a class set that represent a set of elements and support operations like something union intersection negation and the other operations that can be performed on a set so when the type of elements is character we may want to use a simple boolean array of size 256 to make a set for other data types we have to use some other complex technique so an example program for function a template specialization is for example consider uh let's let's say we have a code down here let's just remove it from here and let's say that we have a template class d and then we have a function let's say void f and in this void f we are going to receive every kind of data types that is d a and then what we are going to do is that we are going to simply see out the main template function and then we are going to write in this a variable that is received right in here and then and now to talk about now the specialization function we are going to write in something like template and we are going to specify nothing in these brackets that I told you that this is the syntax for actually doing template specialization and then we are going to write in the very same function that is void f and that is going to be for integer values this time and in this function what we are going to do is that we are going to simply see out specialized template for integer data types and then we are going to simply see out the variable we have received that is a and then and l now down here in the main function what we are going to do is that we are actually going to call in this let's say first we call in it using character values then using integer values and then using float values so this is the possible combinations in which the function f can be called now as you can see right at the top here that this d is expecting any data type if we pass an adding data type this function is going to be called but this function that is the template specialization function it is expecting only integer values so what we are going to do is that we are going to pass in a character to this and integer to this and a float value to this columns at the end and now if I just run this code you can see right here that it says the main template function then it says the temp specialized template for integer data values then it says 10 you can see right in here this is also printed and then it says the main template function 10.28 now if I just explain what happened over here let's just resize it and you can see now right in here let's just scroll it a bit and when the first statement that was this right in here when a character value is passed in this function completely failed so this function was called in and it says the main template function is whatever was written in this function was printed out now when this integer one was called in always remember this preference type that when the specialization function is having an integer value then this function is always going to get a preference over this template function since this is a specialization template function so specialization template function are special so they are going to get a much higher preference than this template normal template function so this is going to be called for the integer values and it is going to print out specialized template for integer data values and then for float one what it's going to do is that it's going to simply call in this at the top because this completely failed because this is expecting an integer value so if you just see here and now what is the purpose of the template specialization actually as you can see that we have a different code or you can say a different line in this case but you can code any other code in this in these brackets of this white function and then in this white function you can also have a different set of code so for any other data type the code is going to be this whatever a set of code is it between these lines but when you have an integer values what you want is that you want a separate code to be executed so this is where the template specialization is going to help you out it is going to execute this code for every other data type but for integer values it will execute the special set of code you want to execute so this is actually the advantage of using template specialization in C plus class so now this was a part of template function specialization now let's move on to the next one that is the class template specialization and let's see some more example programs for a class template specialization so let's just remove this function templates from here remove this main function as well and now let's go on our class template specialization so let's say we have a template that is class t and then we have a class named as testing and in this this testing class what we have that we have a public constructor that is named as testing and in this constructor what we are going to do is that we are going to simply see out normal template constructor and these all are going to be in quotation marks good enough and then on there so this is actually a simple class of this template named as class t and then using that what we have done is that we have made its constructor as public and then simply see how that it is a normal template constructor actually so now what we are going to do is that we are going to make a specialization class so for that what we are going to do is that we are going to use the very same syntax but in case of function this time it is going to be something named as testing and finally we are going to write an int here so what it mean is that now this time this is going to receive every kind of value but this is only going to receive an integer values so when I just made in the object you will understand what is going on so this is also going to have a public constructor and in this public constructor what we are going to have is that we are going to have a simple see out statement that says this specialized template constructor and then this online and now down here in the main function what we are going to do is that we are going to make object the first one is going to be an integer object named as a then we are going to call in testing for this character and the name of the object is going to be b this time then for float values finally and the name of the object is going to be c this time and finally the statement return zero so for the first case what it's going to do is that the testing constructor function is going to be called and the integer values are going to be passed and then for then character values and then float values so let's just run it to see what is the output in this case so you can see right in here that it says specialized template constructor called and then for the remaining two calls from the main function normal template constructor is being called now why is it so because as you can see right in here that the for the first one that says specialized template constructor we have written here integer and as you can see right in here that specialized template constructor or the specialized template class is having this integer right in here so as in the previous case the preference was for specialized template in this case it is for the that as well this template that is the specialized template class is going to take preference over this one that is the normal template class so when this int is going to be passed in it is going to be called in and then for the remaining two that are for character and float this one completely failed because it is expecting integers and this is expecting anything so when it is expecting anything and this fails so this is only the option left so this constructor is called and the remaining two outputs as you can see right in here now this one this is a previous output this one right in here these two says normal template constructor so I hope it is clear now so now you know how function template specialization work and how class template specialization work so let me give you a quick recap of how does specialization actually work so when we write any template based function or class the compiler creates copy of that function or class whenever the compiler sees that being used for a new data type or a new set of data types so if a specialized version is present the compiler first check with the specialized version and then with the main template the compiler first checks with the most specialized version by matching the past parameter with the data types specified in the specialized version so this is why the specialized constructor is always called when it meets the requirement of that specialized function so I hope that this is all clear to you guys so I guess that's it with this tutorial in the next tutorial we are going to be covering about the types of template specialization in C++ that are partial specialization and explicit specialization so for now 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 templates in C++ in this tutorial we are going to be covering about template specialization and we are going to be covering the types of template specialization so from the previous tutorial you already know what is specialization so in this tutorial we are going to be covering the types of specialization that is partial specialization and explicit full specialization for this tutorial we are going to be covering the first type that is the partial specialization so when you instantiate a class template the compiler creates a definition based on the template arguments you have passed in alternatively if all those template arguments match those of an explicit specialization the compiler uses the definition defined by the explicit specialization so basically what is the difference between partial specialization and explicit specialization so a partial specialization basically is a generalization of an explicit specialization an explicit specialization only has a template argument list whereas a partial specialization has both a template argument list and a template parameter list as well The compiler uses the partial specialization if its template argument list matches a subset of the template arguments of a template instantiation. The compiler will then generate a new definition from the partial specialization with the rest of the unmatched template arguments of the template instantiation. You cannot actually partially specialize function templates. In the future tutorial or in the next tutorial, we will be covering explicit full specialization in very detail. For now, you only need to know that partial specialization is a generalization of explicit specialization and partial specialization has an additional list that is the template parameter list along with template argument list, but template argument list are also present in explicit specialization. So to talk about the syntax of partial specialization, you can see right here on your screen, there are two kind of syntax that can be used that the first one is template, then the parametral list, then the class key, then the class headname, then the argument list, and then the declaration, or you can use template, then the parametral list, and then instead of the class key where you can use that you can use the declaration specified sequence, then you can use the declarator then the argument list, and then the initializer. Now here the class headname actually identifies the name of a previously declared class template, and declarator over here identifies the name of a previously declared variable template, and this is used since C++14. Now this declaration must be in the same name space or for member templates, the class scope as the primary template definition is it which it specializes. So let's go over an example for how partial specialization actually work and how they are going to be used for the help of templates. So let's move on to our compiler. So we will have a primary template and then we will have multiple partial templates and you will see how you can use different kind of partial templates. So here we are going to write in a template that is going to be a primary template, and the members for this are going to be class D, then class U that is actually the multiple in templates argument, and then we have an int i, and then we are going to use a structure that is x, which means that it is a general structure in which we have actually a white function. And in the body of that function, what we actually have is that we are going to simply see out that it is a primary template. So as you can see that it is a very simple and easy to understand that it is a straightaway primary template. Now to talk about partial specialization, we are going to use a template that is going to have partial specialization. So it is going to look very much like the first one that is class D. And then let's say we have a single variable that is int i. Now the structure is going to be different and it is going to be the structure that is going to be using this t and this i. And it is going to receive in variables. For example, the sequence for this is let's say t and then let's say a t pointer, which means that the second one is going to be a pointer and then an integer. So in the body of this what we are going to do is in the structure body we'll have a white function. And in this function what we are going to do is that we are going to simply see out partial specialization. Let's name it as number one. So it is partial specialization number one. So that at the end when the output is generated, you can simply differentiate that which part of this template is being executed. Now let's say we have one more template that has a class t as well as a class u and then an integer as well that is int i. And now in the structure what we have different is that first we have to write the name of the structure that is x. Yeah, I guess we also forget to give it a name here. So it is going to be named as x. And now what we are going to do is that we are going to actually change this sequence right in here. At first it was t then t strike and this I now we are going to let's say change it to let's say we have a t pointer. And then we will have a u which means another type of variable and then an integer value. And then in the body of this structure what we are going to do is that we are going to define a function again that is again going to be void punk. And then in the body of that function what we are going to do is that we are going to simply see out partial specialization number two. Alright, now we will have one more template down right here. And it is going to be a let's say simply a class t which means it is also going to have one instance. And then we will have our structure that is structure x. And in that structure x this time let's say we are expecting an integer value, then a t steric. And then another value that is 10. And then in the body of that what we are going to do is that we are going to again have a function that is white funk. And then we are again going to simply see out partial specialization. And it is going to be named as number three. Then in line space. And finally, let's have one more template that also has class t. Then let's say it also has a class u. And it also has an integer i. So we are going to also define a structure for that. And the name is going to be the very same. And then we are going to vary it this as let's say t, the u steric, because in the previous case, it was t steric, then you and then I. So now it is going to be t then you steric and then I. So now in the body of that structure, what we are going to do is that we are going to have the very same thing that is a function in which body we are going to write in c out partial specialization number four. Good enough. And and before going on to a main function, we have to specify this columns at the end of every structure. As you know that syntax for a structure always have a column at this end. So this column over here. And finally, at the end of this template right in here as well. So now let's move on to code our main function and see that how varying the argument is actually going to call the different specialization we have just used. So first let's call over that is the primary template. Let's first call our primary template that is going to be called from x. Then we have to specify an integer, then again an integer. And let's say the second value is 10. And the name of the object is let's say going to be a. So this is going to call our actually primary template. And then what we are going to do is that but we are going to call in our second one, that has a sequence of right up here, as you can see it has a sequence of a simple value than a pointer value and then integer value. So to call it, we are going to write in x, first an integer value, then an integer pointer. And then let's say an integer value that is five and we have to name the object as B. Then for the third one, the sequence is going to be again different. That is right in here a pointer than a gain a variable and then an integer. So it is going to be something like let's say integer value and then let's say the second one that is going to be passed in in that you over there is going to be a float value and then integer value is going to be 10 and the name of the object is going to be let's say C. Then for the next one, if you can see right in here the sequence that is for specialization number three, that is an integer entity steric and then this 10. So it is going to be something like integer and then let's say a character pointer and then again, individuality that is going to be on the name of the pointer is going to be D now. Then for the fourth one, it is going to be normal value than in pointer value and then an integer. So it is going to be something like X and let's say the first value is a float value and the second one is going to be a pointer. So let's make it as an integer pointer and then we are expecting an integer value. So let's make it equals to this 10 and then name of the pointer is going to be D. So now using these pointers that are ABCD and what we are going to do is that we are going to call in the functions in these structures. So it or funk is actually going to call in the function in the structure which is exactly this structure. In our case, it is the structure that is at the top right in here that it's a primary template and then we have partial specialization number one structure that is this then primary specialization number two structure is this then the primary specialization structure number three is there and primary specialization number four structure is right in here. So the a dot funk is going to call in over first one, then B dot Fung is going to call in the second one, then C dot funk is going to call in that specialization number two, then D dot Fung is going to call in specialization number three, and then E dot Fung is finally going to call in specialization number four. So let's just run it and and then I'll explain you that how these are all partial specializations. All right, that's simple, simple mistake I done there. And now let's just run it. And you can see right in here, it says primary template, then partial specialization number one, two, three and four respectively. Now as you can see that these specializations, while they were printed in a sequence, for example, if I just call it in some other sequence, let's call it first, and then call it later. And now run this code, you can see that these have been swapped for is at the top now three is at the bottom. So this all depends on the calling of this function and the structure, because every object has every structure. And since these structures are defined right in here. So if you consider any of this, for example, let's consider this one. In this, why this is a partial specialization because we have a class T, which means we have actually created a data type here that has the type name of T. So if you use this type name T right in here, means first we want to use it for a normal variable, then we want to use it for any pointer variable. And then this entire is right in here, which means that we are actually making something out of it and making a structure out of this, whatever we have initialized right in these brackets of this template. So this is actually called partial specialization in explicit full specialization, you will be seeing that we're going to be using the entire of this function and the entire of these arguments. But now we are actually, since this is a whole lot of, as you can see, it is an ocean of variables, or you can say ocean of variable types. But what we are going to using in here, we are going to use a template variable in a template pointer. And then using this integer, what we are going to do it, we are going to receive a value that is an integer value. So that's why it is called partial specialization. And in here, you can also see that we have now T and U, both, and then an integer i. So we can use any of these to make our structure. So we have taken this T and make it as a pointer and then a U and then this i. So this is partial specialization number two for us. And similarly, you can see right in here that you have this class T. So you can write an int then T steric and then 10. And then we have the steric for this specialization number four in which you have T U and i you can use in T U steric i. And using these structures and the sequence you have specified your structures when you call define the sequence and these pointers and then you call the function, the respective functions of the respective structures are going to be called in. So one more thing before I move on is that you need to understand that each class template partial specialization is separate template. You must provide definition for each member of a class template partial specialization. So moving ahead. So the next thing we need to know is about template parameters and argument list of partial specialization. Now primary templates do not have a template argument list. This list is implied in the template parameter list. Template parameters specified in a primary template, but not used in a partial specialization are omitted from the template parameter list of the partial specialization. The order of a partial specializations argument list is the same as the order of the primary template implied argument list. Now in a template argument list of a partial template parameter, you cannot have an expression that involves non type argument unless that expression is only an identifier. Now if I just show you, you will see that the compiler will not allow the first partial specialization, but it will allow the second partial specialization. Why is it so? Let's see. So let's just remove this from here and remove it up till here. So for example, we have a template, which is right in here. And in that template we have int i and then int j. And then using that what we have is that we have a class that is named as X. Now what will be invalid partial specialization for that class in this case is that if we, for example, write in template int i and then class X and then we write in i steric 4 and let's say I write in i plus 3. And then this is the brackets for the class. And then this column at the end. Now as you can see that this is actually not going to be allowed by your compiler because the type of a non type template argument cannot depend on a template parameter of a partial specialization. The compiler will not allow this thing. So if you change it to something like template and int i using the same argument and then you write in class X very same way. But what you have to do is that you can write in i comma i. And in this case, it is actually allowed. But this one is actually not allowed. And this one right in here is going to be allowed because this is not a type of a non type template argument because it is not dependent on a template parameter of a partial specialization. So a partial specialization template argument list can also not be the same as the list implied by the primary template. And you cannot have default values in template parameter list of partial specialization. And in that case also, it is going to generate an error how it is. For example, if you write in template class T and then you write in T i and then you have a class named as X. And these are the brackets for your class. And then what will be invalid partial specialization in this case is if you write in template, then using the class T what you have to do is that you define in a class X and you write in T and 25. And then these are the brackets for your class. So this is also going to be not allowed because the partial specializations template argument list cannot be the same as the list implied by the primary template. In this case, this is actually the primary template. And this is the partial specialization template, the second one. So in these, you can see that the list implied by the primary template and the specialization template argument are the same. But a partial specialization template argument list, if it is equal to the list implied by the primary template, then it is going to generate errors. So you cannot have default values in the template parameter list of partial specialization, as I told you before. So these are some of the practices that are actually going to be not allowed in partial specialization of classes. So let's move back. And this is the final topic, matching of class template partial specialization. Now the compiler determines whether to use the primary template or one of its partial specialization by matching the template arguments of the class template specialization with the template argument lists of the primary template and partial specializations. Now, how does it's going to check? It's going to check it on three conditions. The first condition is that if the compiler finds only one specialization, then the compiler generates a definition from that specialization. The second rule is that if the compiler finds more than one specialization, then the compiler tries to determine which of the specializations is the most specialized. A template X is more specialized than a template Y. If every argument list that matches the one specified by X also matches the one specified by Y, but not the other way around. If the compiler cannot find the most specialized specialization, then the use of the class templates is ambiguous. The compiler will not allow that program. Now, the third one, third case in is going to be if the compiler, it does not find any matches, then what it's going to do is that the compiler generates a definition from the primary template. And these are actually the three condition. If it finds one specialization, if it's find more than one specialization, and if finds nothing. So these are the three cases of matching of class template partial specialization. So this was all about partial specialization. In the next tutorial, we are going to be covering the second type of specialization that is explicit for specialization. I know these topics are a bit tricky. So you need to go through them again and again, to understand and to clear any kind of ambiguity you have. So for this tutorial, that's all. Thank you so much guys for watching. And I'll see you guys in the next tutorial where we'll be covering the second type of specialization. So bye bye till then. Hey guys, what's up? This is Omar Khan and I welcome you to another tutorial on templates in C++. In this tutorial, we are going to be continuing with template specialization. And we will be seeing the types of template specialization again that are partial specialization and explicit specialization. In the previous tutorial, we already have covered partial specialization in very detail with examples. So in this tutorial, we are going to be studying about explicit or full specialization. So this is actually how the syntax of template explicit specialization look like we have the template, then the empty brackets, then the declaration part. Now here, the template prefix indicates that the following template declaration takes in no template parameters or in other words, it indicates that this is going to be a specialization kind of thing. Moreover, we have the declaration part that can be splitted into two parts that is the declaration name and then the declaration body. The declaration name is actually the name of the previously declared template. And you also have to note in here that you can forward declare an explicit specialization so the declaration body is optional, at least until the specialization is referenced. Now, what can be fully specialized? You can specialize nine kind of things that can be fully specialized, or you can say explicitly specialized that are function templates that are class templates, then you can also explicitly full specialized variable templates, member function of a class template, static data member of a class template, member class of a class template, member enumeration of a class template, member class template of a class or class template, and finally, member function template of a class or class template. So explicit specialization in detail if we talk, it is it may be declared in any scope where its primary template may be defined, which may be different from the scope where the primary template is defined, such as with out of class specialization of member template. Now explicit specialization has to appear after the non specialized template declaration, specialization must be declared before the first use that would cause implicit instantiation in every translation unit, a template specialization that has been declared but not defined can be used just like any other template. Now what is explicit specialization of a function template? When specializing a function template, its template arguments can be omitted if the template argument deduction can provide them from the function arguments. A function with the same name and the same argument list as a specialization is not a specialization. Thirdly, an explicit special specialization of a function template is in line only if it is declared with the inline specifier or defined as delete. It doesn't matter if the primary template is in line or not. Next point is about the default function argument. Default function argument cannot be specified in explicit specialization of function templates, member function templates and member function of class template when the class is implicitly instantiated. An explicit specialization cannot be a friend declaration. And finally, if the primary template has an exception specification that is not false, the explicit specialization must have a compatible exception specialization. So, now let's talk about the members of explicit specialization. When defining a member of an explicitly specialized class template outside the body of the class, the syntax of the specialization that is template and then the empty brackets, it is not used, except if it's a member of an explicitly specialized member class template, which is specialized as a class template because otherwise the syntax would require such definition to begin with the templates and it would require some parameter that are required by the nested template. So, an explicit specialization of a static data member of a template is a definition if the declaration includes an initializer, otherwise it is a declaration. These definitions must use braces for a default initialization. Third point you need to know about explicit specialization member is that a member or a member template of a class template may be explicitly specialized for a given implicit instantiation of a class template, even if the members or member templates is defined in the class template definition. One more thing you need to know is that members or a member template may be nested within many enclosing class templates and an explicitly specialization for such a member, there is always template and then the closing brackets that is the syntax for specialization, it is for every enclosing class template that is explicitly specialized. In such a nested declaration some of the levels may remain unspecialized except that it can specialize a class member template if its enclosing class is unspecialized. For each of these levels the declaration needs arguments as in the previous case because such specializations are themselves templates. So finally let's solve an example of how explicit or full specialization work to clarify and ambiguities and to explain what I've been speaking so far. So let's move on to my compiler that is right in here. So first what we are going to do is that we are going to have a normal template of class t that is equal to float then we have int i equals to five and then we are having a class a in which we will have a public constructor that is named as the name of the class that is a and int value. Now why I have left it you will come to know in a moment. Then we are going to have a specialized template and we are going to write in class a in which we will have public a constructor and finally these columns for the ending of a class and then we will have one more template that is also a specialized template and in this we will have a class that is for double and then in digital value and in the body of that what we will have is that we will also have a public constructor. All right so now right below this what we are going to do is that we are going to have one more template and that is going to be class t int i and we are going to write in a is less than this t and i is greater than a value of i and what we will have is that we will see out primary template and we will see out the value and finally and l. Now as you can see right here that this constructor function that is for class a and we have actually used the very same class that we have initialized right at the top here and we have used integer values and using that value which we have initialized right in here we have actually used that and store in the value of this int i and whenever this template is going to be called it is going to simply print out primary template and this value right in here. Now to talk about a not write in here right after this and this means that now we are initializing the members of this template right in here this take has to be removed from here so what are that we have to actually write in this a which is the constructor function which was declared to be public and what we are going to do in this is that we are going to see out explicit specialization. Now why it is explicit specialization this is because this was initialized this class a that was initialized using a specialization function has only one function that is the constructor function so when we actually use that function right in here it is actually the explicit specialization and what we are going to do is that we are also going to write in double 10 that is right in here which means we are using the second one right in here and then finally and l so this is for the second one and to code this for this first one let's just copy this paste it right here at the top and it is not going to be now like this it is going to be this one and then it is going to simply say explicit specialization and what it's going to say more is that it's also going to say default arguments because as you can see right in here we have default arguments for this one so for the second one that is this one we have also a constructor function and what we have in here was a class right above here that was that a double value and this 10 right in here so this is going to be printed out now what is going to be the scenario of the outputs that are going to be printed and how these are going to be called and when they are going to be called at what call they are actually going to be called because all have the very same net that is a so if you write an int comma any value that is let's say 8 and initialize an object then if you write in let's say a and then let's say an object name it as y and then if you write an a and you write in double comma 10 and name this object as z and now run this code all right I guess some problem with this it says it was previously defined here yes we have to actually write in here double then this 10 so that it looks as a function of this second one that is this one so this constructor function is actually the initialization of this one at the top right in here that has no arguments and then for the second one we have this double and 10 so whenever we are going to use in we are going to write in this a then this double 10 and then a call and call and which means that this a constructor actually belong to this one and then we are going to actually write in this c out statements so whenever an object is initialized and let me just first run you and show you the output of this so you can see right here that it says primary template then explicit specialization of default arguments then explicit specialization of this one so as you can see right at the top here the first one is that is says class t equals to float and then in the i equal to five so when this int was called in which means that the first value is going to be an integer and then this a was passed in and this object was passed in so whenever the constructor function for such a sequence is going to be called the first class template that is the primary template is going to be called in and the constructor function which was initialized right in here is actually going to be called in this statement over here is going to be printed out and secondly when you pass in nothing then this over here that says a and this a is a constructor function of this a and these empty brackets so this constructor function is going to be called in an explicit specialization of default arguments is going to be printed out and then when we you pass in double and ten in the brackets of this a constructor function so this mean that it belong to this constructor function that is of this double and ten so explicit specialization double ten is going to be printed out as you can see right on your output screens as his primary template then eight then explicit specialization of default argument then explicit and you can see one more thing that we have this eight right in over here so this eight was actually passed in from here this eight was made to be changed from here since this was the default value but when a value was passed in this i was changed from i equal to five to i equal to eight and then when it was printed right here that says value i which means that i is now eight so it printed out the value to be equal to eight right from in here because it was passed in from the calling area using this object so i hope it is clear now so the example that was used declared actually to explicit specialization for the primary template the template which is being specialized that is class a the object x uses the constructor of the primary template object y uses the explicit specialization of the constructor function with empty brackets and then object z uses the explicit specialization of arguments that have double and ten so i hope that was well understood and i guess 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 han and i welcome you to the last tutorial on templates in c plus plus so i hope that whatever you have learned so far in this section has been helpful and you have understood each and every concept now i admit that this is a bit tricky and difficult topic i myself have gone through it many times when i was learning it so you don't need to worry if you don't get it the first time go through the tutorial again and again and clear each and every concept and still if you have got any issues then i'll be more than happy to help so i guess that's it with this section of templates thank you so much guys for watching and i'll see you guys in i guess the new section