/*class templates in c++
************************/
/*like function templates a class template is a common class that can represent different similer
classes operating on data of different type.once a class template is define then we can create the
object of the class and replace the user define data type to generic data type.*/
#include<iostream>
using namespace std;
template<typename type1,typename type2>
class simple
{
private:
type1 a;
type2 b;
public:
void getdata()
{
cout<<"please enter the value of a:";
cin>>a;
cout<<"please enter the value of b:";
cin>>b;
}
void display()
{
cout<<"value of a is:"<<a<<endl;
cout<<"value of b is:"<<b<<endl;
}
};
int main()
{
simple<int,int> s1;
simple<float,float> s2;
simple<int,float> s3;
simple<float,int> s4;
cout<<"two integer data type:"<<endl;
s1.getdata();
s1.display();
cout<<"two float data type:"<<endl;
s2.getdata();
s2.display();
cout<<"one integer and one float data type:"<<endl;
s3.getdata();
s3.display();
cout<<"one float and one integer data type:"<<endl;
s4.getdata();
s4.display();
}
No comments:
Post a Comment