运算符重载和模板3.1

/*1. 编写一求两个数的最大值的函数Max,要求用模板实现对任意数据类型数据都可应用该函数求取结果,
在main()函数中分别用整型、实型、字符型数据进行测试。 */
#include<iostream>
using namespace std;

template<typename T>

T Max(T t1,T t2)
{
    if(t1 > t2)
        return t1;
    else
        return t2;
}


int main()
{
    cout << Max(1,2) << endl;
    cout << Max(4.3,2.5) << endl;
    cout << Max('a','b') << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38053395/article/details/80032058