c++day4

#include <iostream>

using namespace std;

class Math
{
    friend const Math operator-(const Math &L, const Math &R);
    friend bool operator>(const Math &L, const Math &R);
    friend Math &operator-=(Math &L, const Math &R);
private:
    int a;
    int b;
public:
    Math(){}
    Math(int a, int b):a(a),b(b)
    {}
    //算术运算符
    const Math operator+(const Math &R) const
    {
        Math temp;
        temp.a = a + R.a;
        temp.b = b + R.b;
        return temp;
    }
    const Math operator*(const Math &R) const
    {
        Math temp;
        temp.a = a * R.a;
        temp.b = b * R.b;
        return temp;
    }
    const Math operator/(const Math &R) const
    {
        Math temp;
        temp.a = a / R.a;
        temp.b = b / R.b;
        return temp;
    }
    const Math operator%(const Math &R) const
    {
        Math temp;
        temp.a = a % R.a;
        temp.b = b % R.b;
        return temp;
    }
    //关系运算符
    bool operator<(const Math &R) const
    {
        if(a < R.a && b < R.b)
        {
            return true;
        }else {
            return false;
        }
    }
    bool operator==(const Math &R) const
    {
        if(a == R.a && b == R.b)
        {
            return true;
        }else {
            return false;
        }
    }
    bool operator<=(const Math &R) const
    {
        if(a <= R.a && b <= R.b)
        {
            return true;
        }else {
            return false;
        }
    }
    bool operator>=(const Math &R) const
    {
        if(a >= R.a && b >= R.b)
        {
            return true;
        }else {
            return false;
        }
    }
    //赋值运算符
    Math &operator+=(const Math &R)
    {
        a += R.a;
        b += R.b;
        return *this;
    }
    Math &operator*=(const Math &R)
    {
        a *= R.a;
        b *= R.b;
        return *this;
    }
    Math &operator/=(const Math &R)
    {
        a /= R.a;
        b /= R.b;
        return *this;
    }
    Math &operator%=(const Math &R)
    {
        a %= R.a;
        b %= R.b;
        return *this;
    }
    void show()
    {
        cout << "a = " << a << "    b = " << b << endl;
    }
};

const Math operator-(const Math &L, const Math &R)
{
    Math temp;
    temp.a = L.a - R.a;
    temp.b = L.b - R.a;
    return temp;
}
bool operator>(const Math &L, const Math &R)
{
    if(L.a > R.a && L.b > R.b)
    {
        return true;
    }else {
        return false;
    }
}
Math &operator-=(Math &L, const Math &R)
{
    L.a -= R.a;
    L.b -= R.b;
    return L;
}
int main()
{
    //算术运算符
    Math m1(99,20);
    Math m2(10,12);
    Math m3 = m1 + m2;
    Math m4 = m1 - m2;
    Math m5 = m1 * m2;
    Math m6 = m1 / m2;
    Math m7 = m1 % m2;
    m3.show();
    m4.show();
    m5.show();
    m6.show();
    m7.show();
    cout << "---------------------------------------" << endl;
    //关系运算符
    if(m1 < m2){
        cout << "m1 < m2" << endl;
    }else if(m1 > m2){
        cout << "m1 > m2" << endl;
    }else if(m1 == m2){
        cout << "m1 == m2" << endl;
    }
    //赋值运算符
    cout << "---------------------------------------" << endl;
    m1 += m2;
    m1.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值