?? 例9.14.txt
字號:
例9.14 聲明一個類模板,利用它分別實現兩個整數、浮點數和字符的比較,求出大數和小數。
#include <iostream>
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 (x<y)?x:y;}
private:
numtype x,y;
};
int main( )
{Compare<int> cmp1(3,7); //定義對象cmp1,用于兩個整數的比較
cout<<cmp1.max( )<<″ is the Maximum of two integer numbers.″<<endl;
cout<<cmp1.min( )<<″ is the Minimum of two integer numbers.″<<endl<<endl;
Compare<float> cmp2(45.78,93.6); //定義對象cmp2,用于兩個浮點數的比較
cout<<cmp2.max( )<<″ is the Maximum of two float numbers.″<<endl;
cout<<cmp2.min( )<<″ is the Minimum of two float numbers.″<<endl<<endl;
Compare<char> cmp3(′a′,′A′); //定義對象cmp3,用于兩個字符的比較
cout<<cmp3.max( )<<″ is the Maximum of two characters.″<<endl;
cout<<cmp3.min( )<<″ is the Minimum of two characters.″<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -