Apart from programming, template is one, which just defines the outline. Consider the leave letter template, which does not contain much information. As a template it contains only from address column but it does not have a from address. similarly it has a body section but it does not have for what purpose the leave is taken.
In c++, templates are much more similar to that. Here, in class template only the class name will be given while declaring. But which datatype the class uses will not be mentioned. This is more generic way of programming.
Then you may ask why there is need for template class?
Here the class does a same functionality for all datatypes. If template class was not there then you have to define the multiple class for each datatype that the class uses. But using templates we are creating only one class.
Real time example:
Consider the Arithemetic operation class. The datatype for the Arithemetic operation can be int, float, double. Inorder to define the multiple class we can just create the single template class and we can make specific whenever we are needed.
In above syntax, the template is keyword just to indicate the class is of type template. Then another new thing is <> brackets. while instantiation, the datatypes that are going to be used are mentioned there. In above we can allowed to instantiate using one type but we can give many as <class type1,class type2>.
DEFINING THE TEMPLATE CLASS:
This is just as normal class definition. In normal class definition you will be using the datatype as int. float etc for declaring the variable. But here you will be using the typename defined inside the angle brackets.
here type1 is identifier. This type1 can be int,float,double etc.,
This is decided while creation of object for the class.
DIFFERENCE BETWEEN CLASS AND TYPENAME IN TEMPLATE:
There is no major technical difference between the class and typename. we can use them interchangeably. But at one rare case it is different. Need to explore that.
CREATING OBJECT FOR TEMPLATE CLASS:
Here the arithmetic_oper is template class name. Here I want to make the class to use the int variable. so I have used int inside the angle brackets. I cannot give more than one type here. If I want to give more than one type then I need to declare the template class as <class type1,class type2>
CREATING TEMPLATE FUNCTION INSIDE THE TEMPLATE CLASS:
Declartion of function template with new type inside the class template goes like this
Here the new type F_Type1 is created .
And the definition of template function goes like this
Here consider this, We have to specify the both templates such as class template c_Type1 and function template type1.
TYPE CASTING OF FUNCTION TEMPLATE:
consider the following snippet,
Here the <int> represent the type casting to parameters. Here both parameter were different. If it was same, then compiler will auto deduct it. we are confusing the compiler in deducting the parameter type. So we explicitly indicate the compiler regarding parameter types. Then (int) is normal type casting. It stores the resultant value in to addition_value variable by typecasting to integer.