C++,运算符重载——关系运算符练习

一、关系运算符重载 > >= < <= == !=

#include 
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 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;
    else
        cout << "a1 != a2" << endl;
    return 0;
}
【输出样例】
a1 < a2
a1 <= a2
a1 != a2

 二、知识点整理

 

你可能感兴趣的:(C++,c++,前端)