10.10作业

运算符重载

#include 

using namespace std;

class Per{
    friend bool operator<(const Per &l, const Per &r);
    friend const Per operator+(const Per &L,const Per &R);
    friend Per &operator-=(Per &l , const Per &r);
private:
    int a;
    int b;
public:
    Per(){}
    Per(int a , int b):a(a),b(b){}
    const Per operator-(const Per &p)const{
        Per temp;
        temp.a = a - p.a;
        temp.b = b - p.b;
        return temp;
    }
     bool operator>(const Per &p)const{
        if(a > p.a && b > p.b)
            return true;
        else
            return false;
    }
     Per &operator+=(const Per &p){
         a += p.a;
         b += p.b;
         return *this;
     }
     bool operator==(const Per &p)const{
        if(a == p.a && b == p.b)
            return true;
        else
            return false;
    }
    void show(){
        cout << "a = " << a << "\tb = " << b < p2)
//        cout << "p1 > p2" << endl;
//    else if(p1 == p2)
//        cout << "p1 == p2" << endl;
//    cout << (p1 > p2) << endl;
//    cout << (p1 < p2) << endl;
//    cout << (p1 == p2) << endl;
    p1+=p2;
    p1.show();
    p2-=p1;
    p2.show();
    return 0;
}

你可能感兴趣的:(c++)