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 F>
F maximum(F first, F second, F third){
F maximum_value =Â first;
if(second>first && second>third){
maximum_value = second;
}
else if(third>first && third > second){
maximum_value = third;
}
return maximum_value;
}
int main(){
// Demostrating that the template function takes values of integer types
int a, b, c;
cout<<"Demostrating that the template function takes values of integer types"<<endl;
cout<<"Enter the first int type value"<<endl;
cin>>a;
cout<<"Enter the second int type value"<<endl;
cin>>b;
cout<<"Enter the third int type value"<<endl;
cin>>c;
int result1 = maximum<int>(a,b,c);
cout<<"The maximum is:\t "<<result1<<endl;
//Demostrating that the template function takes values of double type
double d, e, f;
cout<<"Demostrating that the template function takes values of double types"<<endl;
cout<<"Enter the first double type value"<<endl;
cin>>d;
cout<<"Enter the second double type value"<<endl;
cin>>e;
cout<<"Enter the third double type value"<<endl;
cin>>f;
double result2 = maximum<double>(d,e,f);
cout<<"The maximum is:\t "<<result2<<endl;
//Demostrating that the template function takes values of char types
char g, h, i;
cout<<"Demostrating that the template function takes values of char types"<<endl;
cout<<"Enter the first char type value"<<endl;
cin>>g;
cout<<"Enter the second char type value"<<endl;
cin>>h;
cout<<"Enter the third char type value"<<endl;
cin>>i;
char result3 = maximum<char>(g,h,i);
cout<<"The maximum is:\t "<<result3<<endl;
}
Comments
Leave a comment