运算符重载
给运算符对应一个具体的函数
class Number
{
private:
int x;
int y;
public:
Number(int x, int y)
{
this->x = x;
this->y = y;
}
bool compare(Number& p)
{
return this->x > p.x && this->y > p.y;
}
Number operator++();
Number operator--();
Number operator+(const Number& p);
Number operator-(const Number& p);
Number operator*(const Number& p);
Number operator/(const Number& p);
bool operator>(const Number& p);
bool operator<(const Number& p);
bool operator==(const Number& p);
//等...
};
重载>运算符
#include <iostream>
class Number
{
private:
int x;
int y;
public:
Number(int x, int y)
{
this->x = x;
this->y = y;
}
bool operator>(Number& p)
{
return this->x > p.x && this->y > p.y;
}
};
int main()
{
Number n1(1, 1), n2(2, 2);
bool ret = n1 > n2;
return 0;
}