【C++】用函数模板实现不同类型的两个数比较大小

来源:互联网 发布:淘宝店铺地址哪里看 编辑:程序博客网 时间:2024/06/08 18:45
//用函数模板实现不同类型的两个数比较大小#include <iostream>using namespace std;template <class mytype>  //定义函数模板class Max{public:Max(mytype a, mytype b){x=a;y=b;}    mytype printMax(){return x>y?x:y;}private:mytype x,y;};int main(){Max <int> m1(3,4);Max <float> m2(3.5,4.8);    cout<<m1.printMax()<<endl;cout<<m2.printMax()<<endl;return 0;} 
0 0
原创粉丝点击