#include <iostream> using namespace std; template<class numtype> class Compare {public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; }; template <class numtype> Compare<numtype>::Compare(numtype a,numtype b) {x=a;y=b;} template <class numtype> numtype Compare<numtype>::max() {return (x>y)?x:y;} template <class numtype> numtype Compare<numtype>::min() {return (x<y)?x:y;} int main() {Compare<int> cmp1(3,7); 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); 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'); cout<<cmp3.max()<<" is the Maximum of two characters."<<endl; cout<<cmp3.min()<<" is the Minimum of two characters."<<endl; return 0; }
时间: 2023-10-30 22:00:17 浏览: 69
这段代码实现了一个模板类 Compare,用于比较两个数的大小并输出最大值和最小值。模板类 Compare 中包含一个 numtype 类型的模板参数,用于表示要比较的数的类型。类中定义了构造函数,用于初始化对象的成员变量 x 和 y。类中还定义了两个成员函数 max 和 min,分别用于求出两个数中的最大值和最小值。在主函数中,分别创建了三个 Compare 对象 cmp1、cmp2 和 cmp3,分别用于比较两个整数、两个浮点数以及两个字符的大小,并输出它们的最大值和最小值。最终输出的结果为:
7 is the Maximum of two integer numbers.
3 is the Minimum of two integer numbers.
93.6 is the Maximum of two float numbers.
45.78 is the Minimum of two float numbers.
a is the Maximum of two characters.
A is the Minimum of two characters.
相关问题
#include<iostream> using namespace std; template<class numtype> class Compare { public: Compare(numtype a,numtype b); numtype max( ); numtype min( ); private: numtype x,y; }; //在类模板外定义各成员函数 template<class numtype> Compare<numtype>::Compare(numtype a,numtype b) {x=a;y=b;} template<class numtype> numtype Compare<numtype>::max() {return (x>y)?x:y;} template <class numtype>numtype Compare<numtype>::min() {return(x<y)?x:y;} //主函数 int main() {Compare<int> cmp1(3,7); 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); 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'); cout<<cmp3.max()<<" is the Maximum of two characters."<<endl; cout<<cmp3.min()<<" is the Minimum of two characters."<<endl; return 0; }
这段代码展示了如何使用类模板来创建一个通用的比较类。具体来说,代码中定义了一个类模板 `Compare`,该模板接受一个类型参数 `numtype`,并定义了两个数据成员 `x` 和 `y`,以及三个成员函数:构造函数 `Compare`、`max` 函数和 `min` 函数。构造函数接受两个参数,将它们分别赋值给 `x` 和 `y`。`max` 函数返回 `x` 和 `y` 中的较大值,`min` 函数返回 `x` 和 `y` 中的较小值。
在主函数中,分别使用 `Compare` 类模板创建了三个对象 `cmp1`、`cmp2` 和 `cmp3`,分别用于比较两个整数、两个浮点数和两个字符的大小关系。对于每个对象,分别调用它的 `max` 和 `min` 函数,并在屏幕上输出结果。
由于 `Compare` 类模板是通用的,可以用于比较不同类型的数据,因此在创建对象时需要指定模板参数的类型。在本例中,分别使用了 `int`、`float` 和 `char` 作为模板参数。
#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; }将它改写为在类模板外定义各成员函数。
```
#include<iostream>
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<numtype>::max() {
return (x > y) ? x : y;
}
template<class numtype>
numtype Compare<numtype>::min() {
return (x < y) ? 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;
}
```
阅读全文