1,
#include <iostream>
using namespace std;
class Per{
friend const Per operator+(const Per &c,const Per &d);
friend bool operator>(const Per &c,const Per &d);
friend Per &operator+=(Per &c,const Per &d);
private:
int a;
int b;
public:
Per(){}
Per(int a,int b):a(a),b(b)
{
}
// const Per operator+(const Per &c) const
// {
// Per temp;
// temp.a=a+c.a;
// temp.b=b+c.b;
// return temp;
// }
// bool operator>(const Per &c) const
// {
// if(c.a>a && c.b>b){
// return true;
// }else if(c.a<a && c.b<b){
// return false;
// }
// }
// Per &operator+=(const Per &d)
// {
// a+=d.a;
// b+=d.b;
// return *this;
// }
void show(){
cout << a << endl;
cout << b << endl;
}
};
const Per operator+(const Per &c,const Per &d){
Per temp;
temp.a=c.a+d.a;
temp.b=c.b+d.b;
return temp;
}
bool operator>(const Per &c,const Per &d)
{
if(c.a>d.a && c.b>d.b){
return true;
}else if(c.a<d.a && c.b<d.b){
return false;
}
}
Per &operator+=(Per &c,const Per &d)
{
c.a+=d.a;
c.b+=d.b;
return c;
}
int main()
{
Per s1(10,20);
Per s2(20,30);
Per s3;
s3=s1+s2;
if(s1>s2){
cout << "s1>s2" << endl;
}else{
cout << "s1<s2" << endl;
}
s3.show();
s1 += s2;
s1.show();
return 0;
}
2,思维导图