任务描述 本关任务:有以下程序: , 改写程序为在类模板外定义各成员函数。 相关知识 为了完成本关任务,你需要掌握:类模板的定义与使用。 编程要求 根据提示,在右侧编辑器补充代码。 测试说明 平台会对你编写的代码进行测试: 测试输入:7 3; 预期输出: 7 is the Maximum of two integers. 3 is the Minimum of two integers. 测试输入:93.6 45.78; 预期输出: 93.6 is the Maximum of two float numbers. 45.78 is the Minimum of two float numbers. 测试输入:a A; 预期输出: a is the Maximum of two characters. A is the Minimum of two characters . 开始你的任务吧,祝你成功!
时间: 2023-11-22 20:51:53 浏览: 138
```c++
#include<iostream>
using namespace std;
template<typename T>
class Compare
{
public:
Compare(T a, T b):x(a),y(b){}
T max();
T min();
private:
T x,y;
};
template<typename T>
T Compare<T>::max()
{
return x>y?x:y;
}
template<typename T>
T Compare<T>::min()
{
return x<y?x:y;
}
int main()
{
int a,b;
float c,d;
char e,f;
cin>>a>>b>>c>>d>>e>>f;
Compare<int> cmp1(a,b);
Compare<float> cmp2(c,d);
Compare<char> cmp3(e,f);
cout<<cmp1.max()<<" is the Maximum of two integers. "<<cmp1.min()<<" is the Minimum of two integers. "<<endl;
cout<<cmp2.max()<<" is the Maximum of two float numbers. "<<cmp2.min()<<" is the Minimum of two float numbers. "<<endl;
cout<<cmp3.max()<<" is the Maximum of two characters. "<<cmp3.min()<<" is the Minimum of two characters. "<<endl;
return 0;
}
```
阅读全文