定义一个Complex类,为其定义构造函数,析构函数,

定义一个Complex类,为其定义构造函数,析构函数,试对下列几个运算符进行重载:++,=,!=,+,-,==,其中要求要有成员重载形式和友元重载形式,而且,++运算符要求实现先加和后加两种形式。

 

#include
using namespace std;

class Complex
{
public:

    Complex()
    {
        real = 0;
        imag = 0;
    }
    Complex(double r, double i)
    {
        real = r;
        imag = i;
    }
    Complex operator +(Complex &c2);
    Complex operator -(Complex &c2);
    Complex operator ++();
    Complex operator ++(int);
    friend int operator ==(Complex &c1, Complex &c2);
    friend int operator !=(Complex &c1, Complex &c2);
    Complex operator =(Complex &c2);

    ~Complex()
    {
        cout << "Destructor called!" << endl;
    }
    double real;
    double imag;
};

Complex Complex::operator++()//重载++前缀运算符
{
    ++real;
    ++imag;
    return *this;
}

Complex Complex::operator++(int) //重载++后缀运算符
{
    real++;
    imag++;
    return *this;
}

int operator ==(Complex &c1, Complex &c2)
{
    if(c1.real == c2.real && c1.imag == c2.imag)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

int operator !=(Complex &c1, Complex &c2)
{
    if(c1.real != c2.real || c1.imag != c2.imag)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Complex Complex::operator+(Complex &c2)
{
    Complex c3;
    c3.real = real + c2.real;
    c3.imag = imag + c2.imag;
    return c3;
}

Complex Complex::operator-(Complex &c2)
{
    Complex c3;
    c3.real = real - c2.real;
    c3.imag = imag - c2.imag;
    return c3;
}

Complex Complex::operator =(Complex &c2)
{
    if(this == &c2)
    {
        return *this;
    }
    real = c2.real;
    imag = c2.imag;
    return *this;
}

int main(void)
{
    Complex c(1,2), c1(1,2), c2(2,1), c3 = c1+c2, c4=c2-c1;
    cout << "下面进行判断是否相等" << endl;
    if(c == c1)
    {
        cout << "c = " << c.real << "+" << c.imag << "i" << endl;
        cout << "c1 = " << c1.real << "+" << c1.imag << "i" << endl;
        cout << "c == c1" << endl;
        cout << "判断相等成功" << endl;

    }

    cout << "下面进行判断是否相等" << endl;
    if(c != c2)
    {
        cout << "c = " << c.real << "+" << c.imag << "i" << endl;
        cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
        cout << "c != c2" << endl;
        cout << "判断不相等成功" << endl;

    }

    cout << "进行赋值运算" << endl;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
    c = c2;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
    cout << "赋值成功" << endl;

    cout << "下面进行++后缀运算" << endl;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    c++;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    cout << "++后缀运算成功" << endl;

    cout << "下面进行++前缀运算" << endl;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    ++c;
    cout << "c = " << c.real << "+" << c.imag << "i" << endl;
    cout << "++前缀运算成功" << endl;

    cout << "下面进行+运算符重载" << endl;
    //c3 = c1 + c2;
    cout << "c1 + c2 = " << c3.real << "+" << c3.imag << "i" << endl;
    cout << "进行+运算符重载成功" << endl;

    cout << "下面进行-运算符重载" << endl;
    //c3 = c1 - c2;
    cout << "c2 - c1 = " << c4.real << "+" << c4.imag << "i" << endl;
    cout << "进行-运算符重载成功" << endl;

    return 0;
}

 

你可能感兴趣的:(c++面向对象程序设计)