Tuesday 24 February 2015

Templates in C++

#include<iostream.h>
#include<conio.h>
int max(int a,int b)
{
return(a>b)?a:b;
}
float max(float a,float b)
{
return(a>b)?a:b;
}
char max(char a,char b)
{
return(a>b)?a:b;
}
void main()
{
clrscr();
cout<<max(20,30)<<endl;
cout<<max(4.5,6.7)<<endl;
cout<<max(‘a’,’b’)<<endl;
getch();
}

#include<iostream.h>
#include<conio.h>
template<class T>
Tmax(Tx,Ty)
{
return(x>y)?x:y;
};
void main()
{
clrscr();
cout<<max(17,19)<<endl;
cout<<max(1.5,6.7)<<endl;
cout<<max(‘A’,’B’)<<endl;
getch();
}

#include<iostream.h>
#include<conio.h>
template<class T,class W>
void max(Tx,Wy)
{
if(x>y)
cout<<x<<”is greater than”<<y<<endl;
else
cout<<y<<”is greater than”<<x<<endl;
}
void main()
{
clrscr();
max(10,5.6);
max(‘A’,4.5);
getch();

}

No comments:

Post a Comment