#include <iostream>
using namespace std;
template <class type>
class Compare{
public:
Compare(type a,type b);
type max();
type min();
private:
type x,y;
};
//在类模板外定义成员函数
template <class type>
Compare<type>::Compare(type a, type b) {
x = a;
y = b;
}
template <class type>
type Compare<type>::max() {
return x>y ? x : y;
}
template <class type>
type Compare<type>::min() {
return x<y ? x : y;
}
int main(){
Compare<int>cmp(5,8);
cout << "max: " << cmp.max() << endl;
cout << "min: " << cmp.min() << endl;
return 0;
}
运行结果: