定义复数类complex,并使用友元函数实现复数的加法,减法,乘法,所有函数都返回c

1个回答

  • #include

    using namespace std;

    class complex

    {

    public:

    x05complex(complex &c); //深度复制构造函数

    x05complex(float r,float i); //普通构造函数

    x05void set(float r,float i); //变量重新赋值

    x05complex(); //无参构造函数

    x05friend complex add(complex &b,complex &c); //相加

    x05friend complex sub(complex &b,complex &c); //相减

    friend complex mul(complex &b,complex &c); //相乘

    x05void show();

    private:

    x05float real;

    x05float imag;

    };

    complex::complex()

    {

    x05x05real = 0;

    x05x05imag = 0;

    }

    complex::complex(float r ,float i)

    {

    x05real = r;

    x05imag = i;

    }

    complex::complex(complex &c)

    {

    x05real = c.real;

    x05imag = c.imag;

    }

    void complex::set(float r,float i)

    {

    x05real = r;

    x05imag = i;

    }

    void complex::show()

    {

    x05cout