十月三日作业

#include 
 
using namespace std;
 
//定义一个复数类
class Complex
{
private:
    int real;
    int vir;
    
    
public:
    Complex() {}
    Complex(int r,int v):real(r),vir(v) {}
    
    
    void show()
    {
        if(vir >= 0)
        {
            cout << real << " + " << vir << "i" << endl;
        }
        else
        {
            cout << real << vir << "i" << endl;
        }
    }
    

    //成员函数实现加法运算符重载
    const Complex operator+ (const Complex &R) const
    {
        Complex c;
        c.real = this->real + R.real;
        c.vir = this->vir + R.vir;
        
        return c;
    }
    
 
    //成员函数实现减法运算法重载
    const Complex operator- (const Complex &R) const
    {
        Complex c;
        c.real = this->real - R.real;
        c.vir = this->vir - R.vir;
        
        return c;
    }


    //成员函数实现关系运算符重载
    bool operator> (const Complex &R) const
    {
        return this->real > R.real && this->vir > R.vir;
    }
    
    
    //成员函数实现中括号运算符
    int & operator[] (int index)
    {
        if(index == 0)
        {
            return real;
        }
        else if(index == 1)
        {
            return vir;
        }
    }
    
    
    //成员函数实现+=运算符重载
    Complex & operator+= (const Complex &R)
    {
        this->real += R.real;
        this->vir += R.vir;
        
        return *this;
    }
    
    //成员函数实现负号运算符重载
    Complex operator-()
    {
        Complex c;
        c.real = -this->real;
        c.vir = -this->vir;
        
        return c;
    }
    
    
    //成员函数实现前置自增运算符重载
    Complex &operator++()
    {
        ++this->real;
        ++this->vir;
        
        return *this;
    } 
    

    //成员函数实现后置自增运算符重载
    Complex operator++(int)
    {
        Complex c;
        
        c.real=this->real++;
        c.vir=this->vir++;

        return c;
  
};
 
int main()
{
    Complex c1(6,4);
    c1.show();
    
    
    return 0;
}

你可能感兴趣的:(算法)