3.设计一个复数类,并重载+,-,×,/ 和 <<运算符。写完整程序并调试。

来源:百度知道 编辑:UC知道 时间:2024/06/14 00:30:02

下面是complex.h头文件:
//////////////////////////////////////////////////////////////////////////
// 类名 : Complex
// 产生时间: 2002/10/16
// 所属文件: complex.h
// 功能 : 对复数进行诸如+,-,*,/,+=,-=,*=,/=操作
//
//////////////////////////////////////////////////////////////////////////

#ifndef COMPLEX_H
#define COMPLEX_H

// 修改时间: 2003年7月24日 21:00

#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 -=