Write a template function that returns the maximum value of three elements
passed to the function. Parameters passed to the function can be of int, double,
char but all parameters passed are of the same type at point of time. Parameters
to be passed to the function as are read as input.
#include<iostream>
using namespace std;
template <typename P>
P max(P x, P y , P z){
P value = x;
if(y>x && y>z){
value = y;
}
else if(z>x && z> y){
value = z;
}
return value;
}
int main(){
// Showing the ability of template function working the integer values
int x, y, z;
double q, r, t;
char k, l, m;
cout<<"Showing the ability of template function working the integer values"<<endl;
cout<<"First Integer Value"<<endl;
cin>>x;
cout<<"Second Integer Value"<<endl;
cin>>y;
cout<<"Third Integer value"<<endl;
cin>>z;
int integer_value = max<int>(x,y,z);
cout<<"maximum is:\t "<<integer_value <<endl;
//Showing the ability of template function working the double values
cout<<"Showing the ability of template function working the double values"<<endl;
cout<<"First value of type double"<<endl;
cin>>q;
cout<<"Second value of type double "<<endl;
cin>>r;
cout<<"Third value of type double"<<endl;
cin>>t;
double double_value = max<double>(q,r,t);
cout<<"Maximum :\t "<<double_value<<endl;
//Showing the ability of template function working the double values
cout<<"Template function working the char values"<<endl;
cout<<"First char value"<<endl;
cin>>k;
cout<<"Second char value"<<endl;
cin>>l;
cout<<"Third char value"<<endl;
cin>>m;
char char_value = max<char>(k,l,m);
cout<<"Maximum \t "<<char_value<<endl;
}
Comments
Leave a comment