Template is an more abstract level of a Class by allowing more than one type of data types to be handled by a same class without the need for the definition of more classes.A template class can handle integers,characters,strings etc.For example you can check if a character is greater that a character or an integer is greater than an integer by using a same template class.C++ supports Template classes.To get a better and brighter idea of classes see http://c-madeeasy.blogspot.in/2011/12/what-is-abstractionthe-concept-of.html
The following program is a template class Implementation in C++ to find out the minimum of two integers,characters and floating point numbers.The template class implementation allows this operation to be done using a same class with the need of different classes for different data types.
The following program is a template class Implementation in C++ to find out the minimum of two integers,characters and floating point numbers.The template class implementation allows this operation to be done using a same class with the need of different classes for different data types.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
//c-madeeasy.blogspot.com
template<class T>
T min(T n1,T n2)
{
if(n1<n2)
return n1;
else if(n1>n2)
return n2;
else
cout<<"EQUAL\n\n";
}
void main()
{
int a,b,c,choice,ch;
float d,e,m;
char f,g,n;
clrscr();
do{
cout<<"\t\tMINIMUM\n\n1.Integers\n\n2.Floating Point\n\n3.Character\n\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 : cout<<"\n\nEnter the Integers : ";
cin>>a>>b;
c=min(a,b);
cout<<"Minimum Element : "<<c;
break;
case 2 : cout<<"\n\nEnter the Floating Point numbers : ";
cin>>d>>e;
m=min(d,e);
cout<<"Minimum Element : "<<m;
break;
case 3 : cout<<"\n\nEnter the Characters : ";
cin>>f>>g;
n=min(f,g);
cout<<"Minimum Element : "<<n;
break;
default: cout<<"\n\nInvalid Choice";
}
cout<<"\n\nDo u want to continue?(YES-1,NO-0)";
cin>>ch;
clrscr();
}while(ch!=0);
getch();
}
No comments:
Post a Comment