1.比较运算符重载
#include <iostream>
using namespace std;
class Person
{
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);
private:
int a;
int b;
public:
Person()
{}
Person(int a,int b):a(a),b(b)
{}
void show()
{
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
};
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.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;
}
}
int main()
{
Person P1(13,46);
Person P2(79,85);
Person P3(13,46);
Person P4(85,79);
Person P5(50,50);
if(P1<P2)
{
cout << "P1<P2" << endl;
}
if(P4>P5)
{
cout << "P4<P5" << endl;
}
if(P1==P3)
{
cout << "P1==P3" << endl;
}
if(P1!=P5)
{
cout << "P1!=P5" << endl;
}
return 0;
}
2.思维导图