Write a C++ program to demonstrate CLASS TEMPLATE.
Code :-
/**********************************************************
Program Name - Program for CLASS TEMPLATE
**********************************************************/
#include<iostream.h>
#include<conio.h>
template<class T>
class sum
{
T a,b,c;
public:
void getdata();
void addition();
};
template<class T>
void sum<T> :: getdata()
{
cin>>a>>b;
}
template<class T>
void sum<T> :: addition()
{
c=a+b;
cout<<"\nAddition of two numbers "<<a<<" & "<<b<< " is "<<c<<"\n";
}
void main()
{
clrscr();
sum<int> p;
sum<float> q;
cout<<"\nEnter any two integer numbers :- ";
p.getdata();
p.addition();
cout<<"\nEnter any two float numbers :- ";
q.getdata();
q.addition();
getch();
}
/***************************************************
OUT PUT
Enter any two integer numbers :- 22
78
Addition of two numbers 22 & 78 is 100
Enter any two float numbers :- 22.50
77.50
Addition of two numbers 22.5 & 77.5 is 100
*****************************************************/
Output :-Enter any two integer numbers :- 22 78 Addition of two numbers 22 & 78 is 100 Enter any two float numbers :- 22.50 77.50 Addition of two numbers 22.5 & 77.5 is 100
Enter any two integer numbers :- 22 78 Addition of two numbers 22 & 78 is 100 Enter any two float numbers :- 22.50 77.50 Addition of two numbers 22.5 & 77.5 is 100