友元函数输出不会 是复数运算

定义类Complex(复数类),复数的形式是real+imag i,其中,real和imag分别表示复数的实部和虚部。

(1)该类有三个构造函数:Complex(real,imag)、Complex(real)和Complex()。Complex()生成一个表示原点的复数,Complex(real)生成一个imag为0的复数对象;

(2)对运算符+,-,*,/进行重载(重载为成员函数);

(3)定义友元函数print,能够实现对于复数对象的输出。

编写测试程序,定义两个复数:3.5+5.5i和-3.5+1.0i,输出两个复数的和、差、乘积和商。

友元了函数参数是自己这个类变量。实现写在类外,不用加类域名

代码如下:

#include <iostream>
using namespace std;
class Complex
{
public:
    double real;
    double image;
public:
    Complex(double r,double i)
    {
        real=r; image=i;
    }
    Complex(double r)
    {
        real=r; image=0;
    }
    Complex()
    {
        real=0; image=0;
    }

    Complex operator + (const Complex Right) 
    {
        Complex temp;
        temp.real = real + Right.real;
        temp.image = image + Right.image;
        return temp;
    }
    
    Complex operator - (const Complex Right) {
        Complex temp;
        temp.real = real - Right.real;
        temp.image = image - Right.image;
        return temp;
    }
    
    Complex operator * (const Complex Right) {
        Complex temp;
        temp.real = real * Right.real - image * Right.image;
        temp.image = real * Right.image + image * Right.image;
        return temp;
    }

    Complex operator /(const Complex Right)
    {
        Complex temp;
        temp.real=real/Right.real;
        temp.image =image/Right.real;
        return temp;
    }

    friend void print(Complex comp);

};


void print(Complex comp)
{
    cout<< comp.real << " + "<<comp.image << "i"<<endl;
}

int main()
{
    Complex c1(3.5,5.5);
    Complex c2(-3.5,1.0);

    Complex t1 = c1 + c2; 
    print(t1);
    Complex t2 = c1 - c2;
    print(t2);
    Complex t3 = c1 * c2;
    print(t3);
    Complex t4 = c1 / c2;
    print(t4);
    return 0;
}

#include <iostream>
using namespace std;
class Complex {
        double real, imag;
    public:
        Complex(double r, double i);
        Complex(double r);
        Complex();
        friend void print(Complex com);

        Complex operator+(const Complex& b) {
            Complex c;
            c.real = this->real + b.real;
            c.imag = this->imag + b.imag;
            return c;
        }
        Complex operator-(const Complex& b) {
            Complex c;
            c.real = this->real - b.real;
            c.imag = this->imag - b.imag;
            return c;
        }
        Complex operator*(const Complex& b) {
            Complex c;
            c.real = this->real * b.real - this->imag * b.imag;
            c.imag = this->imag * b.real + this->real * b.imag;
            return c;
        }
        Complex operator/(const Complex& b) {
            Complex c;
            double r1 = this->real, i1 = this->imag, r2 = b.real, i2 = b.imag;
            c.real = (r1 * r2 + i1 * i2) / (r2 * r2 + i2 * i2);
            c.imag = (i1 * r2 - r1 * i2) / (r2 * r2 + i2 * i2);
            return c;
        }
};

Complex::Complex(double r, double i) {
    real = r;
    imag = i;
}
Complex::Complex(double r) {
    real = r;
    imag = 0;
}
Complex::Complex() {
    real = 0;
    imag = 0;
}
void print(Complex com) {
    if(com.imag >= 0) {
        cout << com.real << "+" << com.imag << "i" << endl;
    } else if(com.imag < 0) {
        cout << com.real << com.imag << "i" << endl;
    }
}

int main( ) {
    Complex a(3.5, 5.5);
    Complex b(-3.5, 1.0);
    Complex c;
    print(a);
    print(b);
    cout << "和:";
    c = a + b;
    print(c);
    cout << "差:";
    c = a - b;
    print(c);
    cout << "积:";
    c = a * b;
    print(c);
    cout << "商:";
    c = a / b;
    print(c);
    return 0;
}

img