例9.14クラステンプレートを宣言し、2つの整数、浮動小数点数、文字の比較をそれぞれ実現し、大数と小数を求める.


C++プログラム設計(第三版)譚浩強例9.14個人設計
例9.14クラステンプレートを宣言し、2つの整数、浮動小数点数、文字の比較をそれぞれ実現し、大数と小数を求める.
コードブロック:
クラステンプレート内のメンバー関数の定義
#include 
using namespace std;
template <class numtype>
class Compare
{
public:
    Compare(numtype a, numtype b): x(a), y(b){}
    numtype max()
    {
        return x>y ? x : y;
    }
    numtype min()
    {
        return xprivate:
    numtype x, y;
};
int main()
{
    int m, n;
    cout<<"Please enter m, n: ";
    cin>>m>>n;
    Compare <int>  fa(m, n);
    cout<<"Max="<cout<<"Min="<float s, t;
    cout<<"Please enter s, t: ";
    cin>>s>>t;
    Compare <float> fb(s, t);
    cout<<"Max="<cout<<"Min="<char i, j;
    cout<<"Please enter i, j: ";
    cin>>i>>j;
    Compare <char> fc(i, j);
    cout<<"Max="<cout<<"Min="<"pause");
    return 0;
}

クラステンプレートの外でメンバー関数を定義する
#include 
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::max()
{
    return x>y ? x : y;
}
template <class numtype>
numtype Compare::min()
{
    return xint main()
{
    int m, n;
    cout<<"Please enter m, n: ";
    cin>>m>>n;
    Compare <int>  fa(m, n);
    cout<<"Max="<cout<<"Min="<float s, t;
    cout<<"Please enter s, t: ";
    cin>>s>>t;
    Compare <float> fb(s, t);
    cout<<"Max="<cout<<"Min="<char i, j;
    cout<<"Please enter i, j: ";
    cin>>i>>j;
    Compare <char> fc(i, j);
    cout<<"Max="<cout<<"Min="<"pause");
    return 0;
}