2023.10.10

运算符重载

类外函数实现:

#include 

using namespace std;

class Good
{
    //算数
    friend const Good operator*(const Good &L,const Good &R);
    friend const Good operator+(const Good &L,const Good &R);
    friend const Good operator/(const Good &L,const Good &R);
    friend const Good operator-(const Good &L,const Good &R);
    friend const Good operator%(const Good &L,const Good &R);
    //关系
    friend bool operator>(const Good &L,const Good &R);
    friend bool operator>=(const Good &L,const Good &R);
    friend bool operator<(const Good &L,const Good &R);
    friend bool operator<=(const Good &L,const Good &R);
    friend bool operator==(const Good &L,const Good &R);
    //赋值
    friend Good &operator-=(Good &L,const Good &R);
    friend Good &operator+=(Good &L,const Good &R);
    friend Good &operator*=(Good &L,const Good &R);
    friend Good &operator/=(Good &L,const Good &R);
    friend Good &operator%=(Good &L,const Good &R);
private:
    int n;
    int f;
    int a;
public:
    Good(){}
    Good(int n,int f,int a):n(n),f(f),a(a){}
    void show() const
    {
        cout << "n:" << n << endl;
        cout << "f:" << f << endl;
        cout << "a:" << a << endl;
        //cout << "常" << endl;
    }

};
//非成员
//算数+
const Good operator+(const Good &L,const Good &R)
{
    Good t;
    t.a=L.a+R.a;
    t.n=L.n+R.n;
    t.f=L.f+R.f;
    return t;
}
//算数*
const Good operator*(const Good &L,const Good &R)
{
    Good t;
    t.a=L.a*R.a;
    t.n=L.n*R.n;
    t.f=L.f*R.f;
    return t;
}
//算数-
const Good operator-(const Good &L,const Good &R)
{
    Good t;
    t.a=L.a-R.a;
    t.n=L.n-R.n;
    t.f=L.f-R.f;
    return t;
}
//算数/
const Good operator/(const Good &L,const Good &R)
{
    Good t;
    t.a=L.a/R.a;
    t.n=L.n/R.n;
    t.f=L.f/R.f;
    return t;
}
//算数%
const Good operator%(const Good &L,const Good &R)
{
    Good t;
    t.a=L.a%R.a;
    t.n=L.n%R.n;
    t.f=L.f%R.f;
    return t;
}
//关系运算<
bool operator<(const Good &L,const Good &R)
{
    if(L.a
bool operator>(const Good &L,const Good &R)
{
    if(L.a>R.a && L.n>R.n && L.f>R.f){
        return true;
    }else{
        return false;
    }
}
//关系运算>=
bool operator>=(const Good &L,const Good &R)
{
    if(L.a>=R.a && L.n>=R.n && L.f>=R.f){
        return true;
    }else{
        return false;
    }
}
//关系运算<=
bool operator<=(const Good &L,const Good &R)
{
    if(L.a<=R.a && L.n<=R.n && L.f<=R.f){
        return true;
    }else{
        return false;
    }
}
//关系运算==
bool operator==(const Good &L,const Good &R)
{
    if(L.a==R.a && L.n==R.n && L.f==R.f){
        return true;
    }else{
        return false;
    }
}
//赋值运算-=
Good &operator-=(Good &L,const Good &R)
{
    L.a-=R.a;
    L.n-=R.n;
    L.f-=R.f;
    return L;
}
//赋值运算+=
Good &operator+=(Good &L,const Good &R)
{
    L.a+=R.a;
    L.n+=R.n;
    L.f+=R.f;
    return L;
}
//赋值运算*=
Good &operator*=(Good &L,const Good &R)
{
    L.a*=R.a;
    L.n*=R.n;
    L.f*=R.f;
    return L;
}
//赋值运算/=
Good &operator/=(Good &L,const Good &R)
{
    L.a/=R.a;
    L.n/=R.n;
    L.f/=R.f;
    return L;
}
//赋值运算%=
Good &operator%=(Good &L,const Good &R)
{
    L.a%=R.a;
    L.n%=R.n;
    L.f%=R.f;
    return L;
}

int main()
{
    cout << "-----0-----" << endl;
    Good s0(1,2,3);
    s0.show();
    cout << "-----1-----" << endl;
    Good s1(3,2,1);
    s1.show();
    cout << "----2.0----" << endl;
    Good s2=s0+s1;
    s2.show();
    cout << "----2.1----" << endl;
    s2+=s1;
    s2.show();
    cout << "s2>s1?" << endl;
    bool outcome=s2>s1;
    cout << outcome << endl;
    return 0;
}

类内函数实现:

#include 

using namespace std;

class Good
{
private:
    int n;
    int f;
    int a;
public:
    Good(){}
    Good(int n,int f,int a):n(n),f(f),a(a){}
    void show() const
    {
        cout << "n:" << n << endl;
        cout << "f:" << f << endl;
        cout << "a:" << a << endl;
    }
    //成员
    //算数+
    const Good operator+(const Good &p) const
    {
        Good t;
        t.a=a+p.a;
        t.n=n+p.n;
        t.f=f+p.f;
        return t;
    }
    //算数-
    const Good operator-(const Good &p) const
    {
        Good t;
        t.a=a-p.a;
        t.n=n-p.n;
        t.f=f-p.f;
        return t;
    }
    //算数*
    const Good operator*(const Good &p) const
    {
        Good t;
        t.a=a*p.a;
        t.n=n*p.n;
        t.f=f*p.f;
        return t;
    }
    //算数/
    const Good operator/(const Good &p) const
    {
        Good t;
        t.a=a/p.a;
        t.n=n/p.n;
        t.f=f/p.f;
        return t;
    }
    //算数%
    const Good operator%(const Good &p) const
    {
        Good t;
        t.a=a%p.a;
        t.n=n%p.n;
        t.f=f%p.f;
        return t;
    }
    //关系>
    bool operator>(const Good &R) const
    {
        if(a>R.a && n>R.n && f>R.f){
            return true;
        }else{
            return false;
        }
    }
    //关系<
    bool operator<(const Good &R) const
    {
        if(a=
    bool operator>=(const Good &R) const
    {
        if(a>=R.a && n>=R.n && f>=R.f){
            return true;
        }else{
            return false;
        }
    }
    //关系<=
    bool operator<=(const Good &R) const
    {
        if(a<=R.a && n<=R.n && f<=R.f){
            return true;
        }else{
            return false;
        }
    }
    //赋值+=
    Good &operator+=(const Good &R)
    {
        a+=R.a;
        n+=R.n;
        f+=R.f;
        return *this;
    }
    //赋值=
    Good &operator=(const Good &R)
    {
        a=R.a;
        n=R.n;
        f=R.f;
        return *this;
    }
    //赋值-=
    Good &operator-=(const Good &R)
    {
        a-=R.a;
        n-=R.n;
        f-=R.f;
        return *this;
    }
    //赋值%=
    Good &operator%=(const Good &R)
    {
        a%=R.a;
        n%=R.n;
        f%=R.f;
        return *this;
    }
    //赋值/=
    Good &operator/=(const Good &R)
    {
        a/=R.a;
        n/=R.n;
        f/=R.f;
        return *this;
    }
    //赋值*=
    Good &operator*=(const Good &R)
    {
        a*=R.a;
        n*=R.n;
        f*=R.f;
        return *this;
    }
};

int main()
{
    cout << "-----0-----" << endl;
    Good s0(1,2,3);
    s0.show();
    cout << "-----1-----" << endl;
    Good s1(3,2,1);
    s1.show();
    cout << "----2.0----" << endl;
    Good s2=s0+s1;
    s2.show();
    cout << "----2.1----" << endl;
    s2+=s1;
    s2.show();
    cout << "s2>s1?" << endl;
    bool outcome=s2>s1;
    cout << outcome << endl;
    return 0;
}

2023.10.10_第1张图片

你可能感兴趣的:(c++,算法,前端)