C++
C++8.8作业
完成关系运算符重载,实现成员函数和全局函数的版本
#include <iostream>
using namespace std;
class Person
{
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){}
//成员函数实现关系运算符重载
// bool operator>(const Person &r) const
// {
// if(a > r.a && 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(10,10);
Person p2(20,20);
if(p2 > p1)
{
cout << "p2 > p1" << endl;
}
return 0;
}