直接上代码:
#include<iostream> using namespace std; template<class haha> class Compare { private: haha x, y; public: Compare(haha a, haha b) { x = a; y = b; } haha max() { return (x > y) ? x : y; } haha min() { return (x > y) ? y : x; } }; int main() { Compare <int>c(1, 2); cout << "max=" << c.max() << endl << "min=" << c.min() << endl; Compare <float>d(1.1, 2.2); cout << "max=" << d.max() << endl << "min=" << d.min() << endl; Compare <char>e('a', 'A'); cout << "max=" << e.max() << endl << "min=" << e.min() << endl; }
以比较函数为例子