1)编写复数类,重载加法+、减法-、乘法*等运算符,其中加法+为友元函数,其他为复数类成员函数 2)设计评

1个回答

  • 第一题

    class Complex

    {

    public:

    Complex():real(0),imag(0) {}

    Complex(double r,double i):real(r),imag(i) {}

    friend Complex operator + (Complex &,Complex &);

    Complex operator - ( Complex &);

    Complex operator * ( Complex &);

    void display();

    private:

    double real;

    double imag;

    };

    Complex operator + (Complex &c1,Complex &c2)

    {

    return Complex(c1.real+c2.real,c1.imag+c2.imag);

    }

    Complex Complex::operator - (Complex &c)

    {

    return Complex(real-c.real,imag-c.imag);

    }

    Complex Complex::operator * ( Complex &c)

    {

    Complex temp;

    temp.real = real * c.real - imag * c.imag;

    temp.imag = real * c.imag + c.real * imag;

    return temp;

    }

    void Complex::display()

    {

    cout