In C++ programming language, Template is used to declare a group of function or class in a generic manner.
Generic means that that can process all types of data.
By using template a single function can defined for a group of functions, it is called as a “function template”.
Similarly, when template is associated with classes are known as class template.
Advantage of function template: It avoid unnecessary repetition of source code.
Using template we can create a single function that can process any type of data .
It can accept data of any type such as int, float, long etc.
Syntax for declare class template:
1 | template class < T > class name_of_class {} |
“template” is a keywords.
“template class <t>” statement tells the compiler the following class declaration can use the template data type.
Here T is a variable of template type. It is used to define variable of template (generic) type.
Within the angle bracket <> we declare a variable of template.
We can declare one or more variable separated by comma.
“template” always be global and should not be local means template cannot be declared inside of function or class.
T k; Where , k is variable of type template.
Normal Template Function Declaration
Syntax for Template Function
template class < T > type function_name() { // code
}
Example: Write a program to perform constructor overloading and pass different data type argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; class A { public: A(char c) // constructor with character argument { cout << " c = " << c; } A(int i) // constructor with integer argument { cout << "\n i = " << i; } A(double d) // constructor with double argument { cout << "\n d = " << d; } }; int main() { A x('a'); // pass character data type A y(20); // pass integer data type A z(20.55); // pass double data type } |
Output
1 2 3 | c = a i = 20 d = 20.55 |
In the above program we have created three overloaded constructor, first accept integer data as argument and second accepts float data as argument and third constructor accepts character data as argument.
This approach has following disadvantages
- Re-defining the same function separately for each data type. It increases the requires more time.
- Size of program is increased. Hence, occupies more memory.
- If constructor/function contains bug, it should be correct in every constructor/function.
Example: Write a program to create constructor and pass different data type as argument using template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; template < class T > class xyz { public: xyz(T c) { cout << " c = " << c << endl; } }; int main() { xyz < char > x('a'); // pass character data type xyz < int > y(20); // pass integer data type xyz < double > z(20.55); // pass double data type } |
Output
1 2 3 | c = a c = 20 c = 20.55 |
In the above program we have created a class “xyz” and one constructor it can accept all data type because, constructor contains the variable of template T.
Normal Template Function Declaration
Syntax for Template Function
1 2 | 2 4 |
template class < T > type function_name() { // code
}
Example: Write a program to define normal template function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; template < class T > void show(T x) { cout << "x = " << x << endl; } int main() { char c = 'A'; int i = 10; double d = 20.33; show(c); show(i); show(d); } |
Output
1 2 3 | x = A x = 10 x = 20.33 |
Example: Write a program to find the square of different data type using template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; template < class T > class sqr { public: sqr(T c) // constructor { cout << " square = " << (c * c) << endl; } }; int main() { sqr < int > y(25); //create object y, call constructor & pass integer data type sqr < double > z(15.2); //create object y, call constructor & pass double data type } |
Output
1 2 | square = 625 square = 231.04 |
Class Template With More Parameter
In C++ programming, class template can contain one or more parameter of generic data type.
The argument is separated by comma with template declaration.
Syntax for class template with more than one parameter:
template < class T1, class T2 > class name_of_class {
}
Example: Write a program to define a constructor with multiple template variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; template < class T1, class T2 > class xyz { public: xyz(T1 a, T2 b) { cout << " a = " << a << " b = " << b << endl; } }; int main() { xyz < int, float > x(2, 2.5); xyz < int, char > y(20, 'A'); xyz < float, int > z(3.12, 50); } |
Output
1 2 3 | a = 2 b = 2.5 a = 20 b = A a = 3.12 b = 50 |
Example: Write a program to exchange the value of two variable. Use Template variable as function argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; template < class T > void swap(T * a, T * b) { T temp; temp = * a; * a = * b; * b = temp; } int main() { int x = 5, y = 10; cout << "before swap x = " << x << " & y =" << y; swap( & x, & y); cout << "\n after swap x = " << x << " & y =" << y; double i = 2.22, j = 3.33; cout << "\n before swap i = " << i << " & j =" << j; swap( & i, & j); cout << "\n after swap i = " << i << " & j =" << j; } |
Output
1 2 3 4 | before swap x = 5 & y =10 after swap x = 10 & y =5 before swap i = 2.22 & j =3.33 after swap i = 3.33 & j =2.22 |
Overloading of Template Function
In a C++ programming language, like a function & constructor we can also override the template.
If we create more than one function with same name and one function take template type data as a argument and another function takes primitive data type as a argument then it overrides the function which takes template data as an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; template < class T > void show(T x) { cout << "\n Template variable = " << x; } void show(int i) { cout << "\n Integer variable = " << i; } int main() { show('A'); show(10); show(50.55); } |
Output
1 2 3 | Template variable = A Integer variable = 10 Template variable = 50.55 |
In the above program we have created two function (show ()) with same name.
One function take template type data as a argument and another function takes integer data type as a argument then it overrides the function which takes template data as an argument.