国庆day4---C++运算符重载实现的过程,代码

#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+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;
    }
    bool operator<(const Complex &R)const
    {
        return this->realvirreal+=R.real;
        this->vir+=R.vir;
        return *this;
    }
    //累减法运算符重载
    Complex & operator-=(const Complex &R){
        this->real-=R.real;
        this->vir-=R.vir;
        return *this;
    }
    //前置自增运算符重载
    Complex & operator++(){
        ++this->real;
        ++this->vir;
        return *this;
    }
    //后置自增运算符重载
    Complex operator++(int){
        Complex c;
        c.real=this->real++;
        c.vir=this->vir++;
        return c;
    }
    //中括号运算符重载
    int & operator[](int index){
        if(0==index){
            return real;
        }
        else if(1==index){
            return vir;
        }
    }
    //将全局函数版实现的输出运算符重载函数设置成友元
    friend ostream &operator<<(ostream &L, Complex &c);
};
//输出运算符重载
ostream & operator<<(ostream &L,Complex &c){
    if(c.vir>=0){
        L<

你可能感兴趣的:(国庆假期)