// 类模版
#include<iostream>
using namespace std;
class Compare_int{
public:
Compare_int(int a, int b){
x = a;
y = b;
}
int max(){
return x>y?x:y;
}
int min(){
return x<y?x:y;
}
private:
int x;
int y;
};
class Compare_float{
public:
Compare_float(float a, float b){
x = a;
y = b;
}
int max(){
return x>y?x:y;
}
int min(){
return x<y?x:y;
}
private:
float x;
float y;
};
template<class numtype>
class Compare{
public:
Compare(numtype a, numtype b){
x = a;
y = b;
}
int max(){
return x>y?x:y;
}
int min(){
return x<y?x:y;
}
private:
numtype x;
numtype y;
};
int main(){
int a = 1, b = 2;
float aa = 1.1, bb = 1.2;
Compare_int ci(a,b);
Compare_float cf(aa,bb);
Compare <int> c1(a,b);
Compare <float> c2(aa,bb);
cout << ci.min() << " " << cf.min() << " " << c1.min() << " " << c2.min() << endl;
return 1;
}
【C++】template类的使用方式
最新推荐文章于 2024-07-19 21:09:58 发布