Hello friends, in this post today, we learn about what are the Templates in c++ and why we needs templates. so let's start....
![]() |
Template in c++ |
Template:-
Template is one of the most important and useful feature of C++.
Template provides the idea of generic classes and functions.
Use of one function or class template that works for all data types is generalization.
With the help of templates and functions we can create generic data types and idea leads to generic programming.
In the generic programming generic data types are passed as argument to function and classes.
Some facts about templates are given below:
Template provides the idea of generic classes.
Use of one function or class that works for all data types is generalization.
With the help of templates and functions we can create generic data types and this idea leads to generic programming.
In the generic programming generic data types are passed as argument to function and classes.
A template is similar to a macro which can work for different types of data.
A template created for a function so a function works for variety of data types is termed as function template.
For example a function template max is written which finds maximum of two integer, two floats, two chars etc.
Similarly, when a template is written for a class so one single class works for variety of data types is turnecll as class template.
Types of Template in c++ programming language:-
1. Function Template (generic function).
2. Class template (generic class).
![]() |
Types of template |
Function Template:-
A function template is created when we write one definition of function which works with different type of data types.
A function template does not occupy space in memory.
The actual definition of function template is generated when function is called with specific data type.
The function template does not result in saving memory.
Function template simply relieves us from writing same amount of code for different data types.
The syntax for creating a function template :-
template <class name>
function definition;
Before function definition we write template which is a keyword.
In the brackets < and > we write class and any name which serves as the generic type.
The name may be a single character or a word (similar to identifier). The name usually written in capital but may be in small case too.
Suppose you write a function printData:
Now if you want to print double values or string values, then you have to overload the function:
To perform same operation with different data type, we have to write same code multiple time.
C++ provides templates to reduce this type of duplication of code.
We can now use printData for any data type. Here T is a template parameter that identifies a type. Then, anywhere in the function where T appears, it is replaced with whatever type the function is instantiated.
For example:
Example-
Output:-
Function Templates with Multiple Parameters:
We can use more than one generic data type in the template statement, using comma seperated list as shown below.
For example:
Output:-
Class Template:-
Similar to function template we can create class template.
The difference is simply that earlier we created template for a function now we will be creating it for class.
The class templates are basically used for creating container classes. That is which can contains varieties of objects of any type.
For the class templates definition method remains same but instantiation changes.
Take an example :
Suppose you want to perform addition of two integer number in class.
If you later decide you also want to perform addition of two float numbers in class then you have to write separate class for addition of two numbers.
It means we have to write same class two time because of only different data type. We can solve this problem by using template as follow.
Output:
We will pass data type when we create object of class. So, now template variable „T‟ is replace by data type which we passed with object.
Class Templates with Multiple Parameters:
We can use more than one generic data type in a class template. They are declared as a comma separated list within the template specification as shown below.
Example:
Syntax and examples:-
• The template class can be instantiated when an object of the class will be created. That is :
demo<int>d1;
creates an instance of class template demo for integer data type.
• Similarly
demo<float> d2;
will create an instance of class template demo for float data type. At this instance there will be two different demo classes residing in memory.
One will be using integer data members and other float data members.
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment