Class template with multiple parameters
Prerequisite: Templates in C++
While creating templates, it is possible to specify more than one type. We can use more than one generic data type in a class template. They are declared as a comma-separated list within the template as below:
Syntax:
template<class T1, class T2, ...>
class classname
{
...
...
};
#include<iostream>
using namespace std;
template < class T1, class T2>
class Test
{
T1 a;
T2 b;
public :
Test(T1 x, T2 y)
{
a = x;
b = y;
}
void show()
{
cout << a << " and " << b << endl;
}
};
int main()
{
Test < float , int > test1 (1.23, 123);
Test < int , char > test2 (100, 'W' );
test1.show();
test2.show();
return 0;
}
|
Output:
1.23 and 123
100 and W
Explanation of the code:
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
Last Updated :
27 Jun, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...