C++ 重载关系操作符

#include <iostream>
using namespace std;

class  AAA
{
public:
    AAA() //默认构造
    {

    }
    AAA(int id, string name)
    {
        this->id = id;
        this->name = name;
    }
    ~AAA()//析构
    {
    }
    bool operator > (AAA& a)//重载关系操作符
    {             
        if (this->id > a.id)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
protected:
    //定义成员变量
    int  id;
    string name;

};

int main()
{
  
    AAA a(1, "xiaoming");
    AAA b(2, "xiaogang");

    if (a > b)
    {
        cout << "111\n";
    }
    else
    {
        cout << "222\n";

    }
    return 0;

}
 
 
 
  
 
 

猜你喜欢

转载自www.cnblogs.com/shenji/p/12325224.html
今日推荐