c++day4

1> 思维导图

c++day4_第1张图片

2> 整理代码

#include 

using namespace std;

class Person
{
    friend const Person operator+(const Person &L, const Person &R);
    friend const Person operator-(const Person &L, const Person &R);
    friend const Person operator*(const Person &L, const Person &R);
    friend const Person operator/(const Person &L, const Person &R);
    friend const Person operator%(const Person &L, const Person &R);
    
    
    friend bool operator>(const Person &L, const Person &R);
    friend bool operator>=(const Person &L, const Person &R);
    friend bool operator<(const Person &L, const Person &R);
    friend bool operator<=(const Person &L, const Person &R);
    friend bool operator==(const Person &L, const Person &R);
    friend bool operator!=(const Person &L, const Person &R);
    
    
    friend Person &operator+=(Person &L,const Person &R);
private:
    int a;
    int b;
public:
    Person(){}
    Person(int a, int b):a(a),b(b)
    {}
    
    void show()
    {
        cout << " a = " << a << "    b = " << b << endl;
    }
    
};



//全局函数实现+运算符重载
const Person operator+(const Person &L, const Person &R)
{
    Person temp;
    temp.a = L.a + R.a;
    temp.b = L.b + R.b;
    return temp;
}

//全局函数实现-运算符重载
const Person operator-(const Person &L, const Person &R)
{
    Person temp;
    temp.a = L.a - R.a;
    temp.b = L.b - R.b;
    return temp;
}

//全局函数实现*运算符重载
const Person operator*(const Person &L, const Person &R)
{
    Person temp;
    temp.a = L.a * R.a;
    temp.b = L.b * R.b;
    return temp;
}

//全局函数实现/运算符重载
const Person operator/(const Person &L, const Person &R)
{
    Person temp;
    temp.a = L.a / R.a;
    temp.b = L.b / R.b;
    return temp;
}

//全局函数实现%运算符重载
const Person operator%(const Person &L, const Person &R)
{
    Person temp;
    temp.a = L.a % R.a;
    temp.b = L.b % R.b;
    return temp;
}

//全局函数实现>号运算符重载
bool operator>(const Person &L, const Person &R)
{
    if(L.a>R.a && L.b>R.b)
    {
        return true;
    }
    else
    {
        return false;
    }
}


//全局函数实现>=号运算符重载
bool operator>=(const Person &L, const Person &R)
{
    if(L.a>=R.a && L.b>=R.b)
    {
        return true;
    }
    else
    {
        return false;
    }
}


//全局函数实现<号运算符重载
bool operator<(const Person &L, const Person &R)
{
    if(L.ap2)
    {
        cout << "p3>p2" << endl;
    }
    
    if(p3>=p2)
    {
        cout << "p3>=p2" << endl;
    }
    
    if(p3

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