Monday, January 20, 2014

C++ Program to illustrate Class Template

with 0 Comment
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class sample
{
T1 a;
T2 b,c,d;
public:
sample(T1 a1,T2 b1)
{
a=a1;
b=b1;
}
void add()
{
c=a+b;
d=a*b;
}
void display()
{
cout<<"\n\n Addition:"<<c<<"\n";
cout<<"\n\n Multiplication:"<<d<<"\n";
}
};

void main()
{
int x,x1;
float y,y1;
clrscr();
cout<<"\t\t CLASS TEMPLATE WITH MULTIPLE PARAMETERS \n";
cout<<"\t\t ---------------------------------------------- \n";
cout<<"\n Enter the x integer value:";
cin>>x;
cout<<"\n Enter the y float value:";
cin>>y;
sample<int,float>s1(x,y);
s1.add();
s1.display();
cout<<"\n Enter the y1 float value:";
cin>>y1;
cout<<"\n Enter the x1 integet value:";
cin>>x1;
sample<float,int>s2(y1,x1);
s2.add();
s2.display();
getch();
}


-------------
OUTPUT:
-------------
CLASS TEMPLATE WITH MULTIPLE PARAMETERS
---------------------------------------------------------------------
Enter the x integer value:5
Enter the y float value:3.2

Addition :8.2
Multiplication:16

Enter the y1 float value:4.5
Enter the x1 integer value:3

Addition : 7
Multiplication :13

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive