#include<iostream>
using namespace std;
//类模板外定义各成员函数
template<class numtype>
class Compare
{
public:
Compare(numtype a, numtype b)
{
x = a;
y = b;
}
numtype max();
numtype min();
private:
numtype x, y;
};
template<class numtype> //每一个成员函数的类外定义,都要这句话
numtype Compare<numtype>::max()
{
return (x > y) ? x : y;
}
template<class numtype>
numtype Compare<numtype>::min()
{
return (x < y) ? x : y;
}
int main()
{
Compare<int> cmp1(3, 7);
cout << cmp1.max() << "是两个数中大的." << endl;
cout << cmp1.min() << "是两个数中小的." << endl;
Compare<double> cmp2(45.78, 93.6);
cout << cmp2.max() << "是两个数中大的." << endl;
cout << cmp2.min() << "是两个数中小的." << endl;
Compare<char> cmp3('a', 'A');
cout << cmp3.max() << "是两个数中大的." << endl;
cout << cmp3.min() << "是两个数中小的." << endl;
return 0;
}
谭浩强c++第三版9-12
最新推荐文章于 2024-09-09 12:31:15 发布