Answer to Question #204734 in C++ for Mahi

Question #204734

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;

}


1
Expert's answer
2021-06-08T23:49:36-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog