描述 编写类模板,类体内定义成员函数,使计算器可以实现不同类型数据(int,float等)的加、减、乘、除运算。(类体内实现) 主函数为: int main() { Calculate cal1; float x1=1.1,y1=2.2; cout<<cal1.add(x1,y1)<<endl; cout<<cal1.mimus(x1,y1)<<endl; cout<<cal1.multiply(x1,y1)<<endl; cout<<cal1.divide(x1,y1)<<endl; Calculate cal2; int x2=1,y2=2; cout<<cal2.add(x2,y2)<<endl; cout<<cal2.mimus(x2,y2)<<endl; cout<<cal2.multiply(x2,y2)<<endl; cout<<cal2.divide(x2,y2)<<endl; return 0; } 输入 NULL 输出 3.3 -1.1 2.42 0.5 3 -1 2 0 样例输入 NULL 样例输出 3.3 -1.1 2.42 0.5 3 -1 2 0
时间: 2023-08-20 19:11:36 浏览: 91
C++ 实现一个简单的计算器 类
4星 · 用户满意度95%
```cpp
#include <iostream>
using namespace std;
template<typename T>
class Calculate {
public:
T add(T a, T b) {
return a + b;
}
T minus(T a, T b) {
return a - b;
}
T multiply(T a, T b) {
return a * b;
}
T divide(T a, T b) {
return a / b;
}
};
int main() {
Calculate<float> cal1;
float x1 = 1.1, y1 = 2.2;
cout << cal1.add(x1, y1) << endl;
cout << cal1.minus(x1, y1) << endl;
cout << cal1.multiply(x1, y1) << endl;
cout << cal1.divide(x1, y1) << endl;
Calculate<int> cal2;
int x2 = 1, y2 = 2;
cout << cal2.add(x2, y2) << endl;
cout << cal2.minus(x2, y2) << endl;
cout << cal2.multiply(x2, y2) << endl;
cout << cal2.divide(x2, y2) << endl;
return 0;
}
```
阅读全文