一、关系运算符重载 > >= < <= == !=
#include <iostream>
using namespace std;class Relates
{
private:int a;int b;
public:Relates() {}Relates(int a,int b):a(a),b(b) {}bool operator>(const Relates &R) const{if((a>R.a&&b>R.b) || (a==R.a&&b>R.b) || (a>R.a&&b==R.b))return true;return false;}bool operator>=(const Relates &R) const{if(a>=R.a && b>=R.b)return true;return false;}bool operator<(const Relates &R) const{if((a<R.a&&b<R.b) || (a==R.a&&b<R.b) || (a<R.a&&b==R.b))return true;return false;}bool operator<=(const Relates &R) const{if(a<=R.a && b<=R.b)return true;return false;}bool operator==(const Relates &R) const{if(a==R.a && b==R.b)return true;return false;}
};
int main()
{Relates a1(10,20);Relates a2(20,20);if(a1 > a2)cout << "a1 > a2" << endl;if(a1 >= a2)cout << "a1 >= a2" << endl;if(a1 < a2)cout << "a1 < a2" << endl;if(a1 <= a2)cout << "a1 <= a2" << endl;if(a1 == a2)cout << "a1 == a2" << endl;elsecout << "a1 != a2" << endl;return 0;
}
【输出样例】
a1 < a2
a1 <= a2
a1 != a2
二、知识点整理