Study the myMAX function provided below. You are required to create a C++ template based myMAX function and test it on different built-in data types.
//Make a template out of this function. Don't forget the return type.
int myMax(int one, int two)
{
int bigger;
if(one < two)
bigger = two;
else
bigger = one;
return bigger;
}
int main()
{
int i_one = 3, i_two = 5;
cout <<"The max of "<< i_one <<" and "<< i_two <<" is "
<< myMax(i_one, i_two) << endl;
//Test your template on float and string types
return 0;
}
#include <iostream>
using namespace std;
template <class T>
T myMax (T one, T two) {
T bigger;
if(one < two)
bigger = two;
else
bigger = one;
return bigger;
}
int main()
{
int i_one = 3, i_two = 5;
cout <<"The max of "<< i_one <<" and "<< i_two <<" is "<< myMax<int>(i_one, i_two) << endl;
float a=23.89, b=13.45;
cout <<"The max of "<<a <<" and "<< b <<" is "<< myMax<float>(a,b) << endl;
string x="Hello",y="world";
cout <<"The max of "<<x <<" and "<< y <<" is "<< myMax<string>(x,y) << endl;
return 0;
}
Comments
Leave a comment