运行结果为加减法怎么改
时间: 2023-10-01 20:11:33 浏览: 21
抱歉,我理解错了,下面是修改后的代码和运行结果:
```cpp
#include <iostream>
using namespace std;
class complex {
private:
double real; // 实部
double imag; // 虚部
public:
complex(double r = 0, double i = 0); // 构造函数
void show(); // 数据显示函数
friend complex operator+(const complex& c1, const complex& c2); // 重载加号运算符
friend complex operator-(const complex& c1, const complex& c2); // 重载减号运算符
};
complex::complex(double r, double i) {
real = r;
imag = i;
}
void complex::show() {
cout << "(" << real << ", " << imag << "i)" << endl;
}
complex operator+(const complex& c1, const complex& c2) {
return complex(c1.real + c2.real, c1.imag + c2.imag);
}
complex operator-(const complex& c1, const complex& c2) {
return complex(c1.real - c2.real, c1.imag - c2.imag);
}
int main() {
complex c1(7, 8), c2(5, 10);
(c1 + c2).show();
(c1 - c2).show();
return 0;
}
```
运行结果:
```
(12, 18i)
(2, -2i)
```
修改后只保留了加、减法的运算符重载,去掉了乘、除法的重载。