Question #224676

Create a generic function max that gives the maximum value of three generic type arguments that are passed to it. Then test this function by calling it with char, int, and float type. (Please provide executeable code in C++).


Expert's answer

template<typename T>
T max(T v1, T v2, T v3)
{
    T m = v1 >= v2 ? v1 : v2;
    return m >= v3 ? m : v3;
}

int main()
{
    char  maxChar  = max('b', 'c', 'a');
    int   maxInt   = max(2, 3, 1);
    float maxFloat = max(2.0f, 3.0f, 1.0f);
    
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS