声明一个类的模板的时候,在定义之前增加一行
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/untitled5
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.
7is the Maximum of two integer numbers.
3is the Minimum of two integer numbers.
Process finished with exit code 0**
730

被折叠的 条评论
为什么被折叠?



