Operadores de Comparación:
==
, !=
, <
, >
, <=
, >=
Se utilizan para comparar dos valores y producir un resultado booleano (verdadero o falso) en función de la relación entre esos valores.
MyNum.hppcpp
MyNum.cppclass MyNum
{
private:
int _num;
public:
MyNum();
MyNum(const MyNum &);
~MyNum();
MyNum &operator=(const MyNum &);
int getNum() const;
void setNum(const int);
bool operator>(const MyNum &) const;
bool operator<(const MyNum &) const;
bool operator>=(const MyNum &) const;
bool operator<=(const MyNum &) const;
bool operator==(const MyNum &) const;
bool operator!=(const MyNum &) const;
};
cpp
main.cppMyNum::MyNum() : _num(0) {}
MyNum::MyNum(const MyNum &tmp) { _num = tmp._num; }
MyNum::~MyNum() {}
int MyNum::getNum() const { return _num; }
void MyNum::setNum(const int num) { _num = num; }
MyNum &MyNum::operator=(const MyNum &other)
{
if (this != &other)
_num = other._num;
return *this;
}
bool MyNum::operator<(const MyNum &other) const { return _num < other._num; }
bool MyNum::operator>(const MyNum &other) const { return _num > other._num; }
bool MyNum::operator>=(const MyNum &other) const { return _num >= other._num; }
bool MyNum::operator<=(const MyNum &other) const { return _num <= other._num; }
bool MyNum::operator==(const MyNum &other) const { return _num == other._num; }
bool MyNum::operator!=(const MyNum &other) const { return _num != other._num; }
cpp
outputint main()
{
MyNum a, b;
a.setNum(42);
b.setNum(21);
std::cout << "a is " << a.getNum() << std::endl;
std::cout << "b is " << b.getNum() << std::endl;
std::cout << "a > b: " << (a > b) << std::endl;
std::cout << "a < b: " << (a < b) << std::endl;
std::cout << "a >= b: " << (a >= b) << std::endl;
std::cout << "a <= b: " << (a <= b) << std::endl;
std::cout << "a == b: " << (a == b) << std::endl;
std::cout << "a != b: " << (a != b) << std::endl;
return 0;
}
shell
a is 42
b is 21
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
a == b: 0
a != b: 1