//用函数模板实现不同类型的两个数比较大小 #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; } <img src="http://img.blog.csdn.net/20150507203501196?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-12-25 20:16:36