类的友元重载函数构造复数进行计算

为什么类里的析构函数delete去掉就可以正确得到,而加上就出现如图二的错误了呢?想了一下午还是想不明白?(┯_┯)!

img

#include<iostream>
using namespace std;
class complex
{
    double *p;
public:
    complex(double m=0,double n=0)
    {
        p=new double[2];
        p[0]=m;p[1]=n;
    }
    //complex(){}
    complex(const complex &obj)
    {
        p[0]=obj.p[0];
        p[1]=obj.p[1];
    }
    ~complex()
    {
        delete[]p;
    }
    friend complex operator+ ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]+c2.p[0];
        double i=c1.p[1]+c2.p[1];
        return complex(r,i);
    }
    friend complex operator- ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]-c2.p[0];
        double i=c1.p[1]-c2.p[1];
        return complex(r,i);
    }
    friend complex operator* ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]*c2.p[0]-c1.p[1]*c1.p[1];
        double i=c2.p[0]*c1.p[1]+c2.p[1]*c1.p[0];
        return complex(r,i);
    }
    void show()
    {
        if(p[0]!=0)
        {
            cout<<p[0];
            if(p[1]>0&&p[1]!=1)
            {
                cout<<'+'<<p[1]<<'i'<<endl;
            }
            else if(p[1]<0&&p[1]!=-1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]==1)
                cout<<'+'<<'i'<<endl;
            else if(p[1]==-1)
                cout<<'-'<<'i'<<endl;
            else
                cout<<endl;
        }
        else
        {
            if(p[1]>0&&p[1]!=1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]<0&&p[1]!=-1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]==1)
                cout<<'i'<<endl;
            else if(p[1]==-1)
                cout<<'-'<<'i'<<endl;
        }
    }
};

int main()
{
    double q,p,s,e;
    cin>>q>>p>>s>>e;
    complex a(q,p),b(s,e),c;
    char d;
    cin>>d;
    if(d=='-')
        c=a-b;
    else if(d=='+')
        c=a+b;
    else if(d=='*')
        c=a*b;
    c.show();
    return 0;
}

img

这里尽量别用指针变量作为成员,会有很多问题(深拷贝等),就用两个成员,一个实部一个虚部就行了

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;

class Complex
{
public:
    Complex(double real = 8.0, double image = 6.0)       //构造函数
        :_real(real)
        , _image(image)
    {
        //cout << " Complex(double real, double image)" << endl;
    }
    Complex(const Complex& d)          //拷贝函数
    {
        //cout << "Complex(const Complex& d)" << endl;
        _real = d._real;
        _image = d._image;
    }
    ~Complex()       //析构函数
    {
        //cout << "~Complex() " << endl;
        _real = 0.0;
        _image = 0.0;
    }
    Complex& operator=(const Complex& d)         //赋值运算符重载
    {
        //cout << "=" << endl;
        if (this != &d)
        {
            _real = d._real;
            _image = d._image;
        }
        return *this;
    }
    void show()const                //打印复数
    {
        cout << _real << "+" << _image<<"i" << endl;
    }

    bool operator==(const Complex& d)             //==
    {
        cout << "==" << endl;
        return this->_real == d._real
            && this->_image == d._image;
    }
    bool operator!=(const Complex& d)             //!=
    {
        cout << "!=" << endl;
        return this->_real != d._real
            || this->_image == d._image;
    }
    //复数只有当虚部为0时,即_image=0时,才可以比较大小,这时比较的是实部即_real的大小
    bool operator>(const Complex& d)          //>
    {
        if (this->_image != 0 || d._image != 0)
        {
            cout << "无法比较       ";
            return false;
        }

        else
        {
            return this->_real > d._real;
        }
    }
    bool operator<(const Complex& d)           //<
    {
        if (this->_image != 0 || d._image != 0)
        {
            cout << "无法比较      ";
            return false;
        }
        else
        {
            return this->_real < d._real;
        }
    }
    bool operator<=(const Complex& d)           //<=
    {
        if (this->_image != 0 || d._image != 0)
        {
            cout << "无法比较      ";
            return false;
        }
        else
        {
            return this->_real <= d._real;
        }
    }
    bool operator>=(const Complex& d)           //>=
    {
        if (this->_image != 0 || d._image != 0)
        {
            cout << "无法比较        ";
            return false;
        }
        else
        {
            return this->_real >= d._real;
        }
    }
    Complex operator+ (const Complex& d)           //+
    {
        //cout << "+" << endl;
        Complex ret;
        ret._real = (this->_real + d._real);
        ret._image = (this->_image + d._image);
        return ret;
    }
    Complex operator- (const Complex& d)           //-
    {
        //cout << "-" << endl;
        Complex ret;
        ret._real = (this->_real - d._real);
        ret._image = (this->_image - d._image);
        return ret;
    }
    Complex operator* (const Complex& d)           //*
    {
        //cout << "*" << endl;
        Complex ret;
        ret._real = (this->_real * d._real);
        ret._image = (this->_image * d._image);
        return ret;
    }
    Complex operator / (const Complex& d)           //
    {
        //cout << "/" << endl;
        Complex ret;
        if (d._real == 0 || d._image) return d;
        ret._real = (this->_real / d._real);
        ret._image = (this->_image / d._image);
        return ret;
    }
    Complex& operator+=(const Complex& d)          //+=
    {
        cout << "+=" << endl;
        this->_real += d._real;
        this->_image += d._image;
        return *this;
    }
    Complex& operator++()            //前置++
    {
        cout << "前置++" << endl;
        this->_real += 1;
        return *this;
    }
    Complex operator++(int)         //后置++
    {
        cout << "后置++" << endl;
        Complex tmp(*this);
        this->_real += 1;
        return tmp;
    }


protected:
    double _real;
    double _image;
};

void Test1()
{
    Complex d1;
    Complex d2(4.0, 9.6);
    d1.show();
    d2.show();
    d1 += d2;
    d1.show();
    d2.show();
}
void Test2()
{
    double q, p, s, e;
    cin >> q >> p >> s >> e;
    Complex a(q, p), b(s, e), c;
    char d;
    cin >> d;
    if (d == '-')
        c = a - b;
    else if (d == '+')
        c = a + b;
    else if (d == '*')
        c = a * b;
    c.show();

}
int main()
{
    Test2();
    return 0;
}