求助——定义复数类complex,重载运算符“+”的C++程序题

来源:百度知道 编辑:UC知道 时间:2024/07/04 02:29:08
定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。

是来这里求简洁正确答案的,希望大家能帮忙的帮帮忙哦。。。
真的很感谢这里的朋友都给出了自己认为合理而且完整的答案,谢谢了拉。。。

#include <iostream>
#include <cmath>
class Complex
{

public:

Complex() : _real(0), _imag(0) {}
explicit Complex( double r) : _real(r), _imag(0) {}
Complex(double r, double i) : _real(r), _imag(i) {}

Complex& operator+=(const double& d)
{
_real += d;
return *this;
}

Complex& operator+=(const Complex& c)
{
_real += c._real;
_imag += c._imag;
return *this;
}

Complex& operator-=(const double &d)
{
_real -= d;
return *this;
}

Complex& operator-=(const Complex& c)
{
_real -= c._real;
_imag -= c._imag;
return *this;
}

Complex& operator*=(const double& d)
{
_real *= d;
_imag *= d;
return *this;
}

Complex& operator*=(const Complex& c)
{
double re = _real;
double im = _imag;
_real = re * c._real -