Following Program Illustrate The Use Of Template Class With Multiple Parameters:
#include<iostream>
using namespace std;
template<class T1=int,class T2=int>
class sample
{
T1 a;
T2 b;
public:
sample(T1 x,T2 y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
cout<<"\n********************";
}
};
main()
{
sample<int,float>a(12,76.23);//passing int and float
a.display();
sample<float,int>b(56.7,76);//passing float and int
b.display();
sample<float>c(15.7,12);//passing one type and other is default
c.display();
sample<>d('a',13);//default types used
d.display();
}
#include<iostream>
using namespace std;
template<class T1=int,class T2=int>
class sample
{
T1 a;
T2 b;
public:
sample(T1 x,T2 y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
cout<<"\n********************";
}
};
main()
{
sample<int,float>a(12,76.23);//passing int and float
a.display();
sample<float,int>b(56.7,76);//passing float and int
b.display();
sample<float>c(15.7,12);//passing one type and other is default
c.display();
sample<>d('a',13);//default types used
d.display();
}
Output: