习题 9.12 将例9.14改写为在类模板外定义各成员函数。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/navicheung/article/details/82491013

C++程序设计(第三版) 谭浩强 习题9.12 个人设计

习题 9.12 将例9.14改写为在类模板外定义各成员函数。

代码块:

#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> cmpl(3, 7);
    cout<<cmpl.max()<<" is the Maximum of two integer numbers."<<endl;
    cout<<cmpl.min()<<" is the Minimum of two integer numbers."<<endl;
    Compare <float> cmp2(45.78, 93.6);
    cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
    cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl;
    Compare <char> cmp3('a', 'A');
    cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
    cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/82491013
今日推荐