【day11.03】C++运算符重载实现的过程,代码

#include 

using namespace std;

//定义一个负数类   5+3i
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;
        }
    }

    //全局函数版实现加运算符重载
    friend const Complex operator+ (const Complex &L,const Complex &R);

    //成员函数版实现运算符重载
    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;
    }

    //将全局函数实现的输出运算符重载函数设置友元
    friend ostream &operator<<(ostream &L,Complex &c);

    //重载小括号运算符,做一个仿函数
    void operator()(string s)
    {
        cout<<s<<endl;
    }

    //重载小括号运算符,做一个仿函数
    void operator()()
    {
        cout<<520<<" "<<1314<<endl;
    }


};

//全局函数版实现加号运算符重载:实部+实部  虚部+虚部
const Complex operator+ (const Complex &L,const Complex &R)
{
    //定义一个临时空间
    Complex c;
    c.real=L.real+R.real;
    c.vir=L.vir+R.vir;
}

// 重载输出运算符函数
ostream &operator<<(ostream &L,Complex &c)
{
    if(c.vir>=0)
    {
        L<<c.real<<"+"<<c.vir<<"i"<<endl;
    }else {
        L<<c.real<<c.vir<<"i"<<endl;
    }
    return L;
}

int main()
{
    Complex c1(5,3);
    c1.show();              //5+3i

    Complex c2(2,-1);
    c2.show();              //2-1i

    Complex c3=c1-c2;       //调用减法运算符重载函数

    c3.show();              //3+4i

    if(c3>c2)
    {
        cout<<"yes"<<endl;
    }else {
        cout<<"no"<<endl;
    }

    c3[0]=5;
    c3.show();              //5+4i

    c3 += c2;
    c3.show();

    Complex c4=-c3;
    c4.show();
    c3.show();

    Complex c5=++c3;        //调用前置自增运算符重载函数
    c5.show();              //8+4i
    c3.show();              //8+4i

    Complex c6=-c3++;       //调用后置自增运算符重载函数
    c6.show();              //8+4i
    c3.show();              //9+5i

    cout<<"******************"<<endl;
    cout<<c3;               //cout.operator<<(c3)

    c3("hello world");
    c3();

   /* string s="hello world";
    s[0];   */                //s.operator[](0)

    return 0;
}

你可能感兴趣的:(c++,算法,开发语言)