【问题描述】定义复数类的加法与减法,使之能够执行下列运算:
Complex a(2,5), b(7, 8), c(0, 0);
c=a+b;
c=4.1+a;
c=b-5.6;
【输入形式】无
【输出形式】各复数运算结果
【样例输入】无
【样例输出】
9+i13
6.1+i5
1.4+i8
【样例说明】
【评分标准】 1个评分点
#include <iostream>
using namespace std;
//定义复数类
class Complex {
private:
double real;
double imag;
public:
//构造函数初值为0
Complex(double r = 0.0, double i = 0.0) {
this->real = r;
this->imag = i;
}
//对+进行重载
Complex operator+(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
//对-进行重载
Complex operator-(const Complex& c) const {
return Complex(real - c.real, imag - c.imag);
}
//对<<进行重载,利
这篇博客介绍了如何在C++中定义复数类,并重载加法和减法运算符,以实现复数之间的加减操作。通过示例展示了如何使用重载的运算符进行复数运算,并给出了运算结果。
订阅专栏 解锁全文
2369

被折叠的 条评论
为什么被折叠?



