类的模板

来源:互联网 发布:尼泊尔淘宝 编辑:程序博客网 时间:2024/06/05 07:21

声明一个类的模板的时候,在定义之前增加一行
template

**#include <iostream>using namespace std;template <typename 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> 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(3.2323,7.658);    cout<<cmpl.max()<<"is the Maximum of two integer numbers."<<endl;    cout<<cmpl.min()<<"is the Minimum of two integer numbers."<<endl;    Compare <char> cmp3('A','G');    cout<<cmpl.max()<<"is the Maximum of two integer numbers."<<endl;    cout<<cmpl.min()<<"is the Minimum of two integer numbers."<<endl;    return 0;} **
**/home/andrew/文档/Clion/untitled5/cmake-build-debug/untitled57is the Maximum of two integer numbers.3is the Minimum of two integer numbers.7is the Maximum of two integer numbers.3is the Minimum of two integer numbers.7is the Maximum of two integer numbers.3is the Minimum of two integer numbers.Process finished with exit code 0**
0 0
原创粉丝点击