Operator Overloading: Overloading Comparison Operator as Member Function
What is it?
In C++, the comparison operators (==
, !=
, <
, >
, <=
, >=
) can be overloaded for user-defined types such as classes and structs. Overloading these operators allow objects of user-defined types to be compared in a way that is natural and intuitive. This operator overloading is done using member functions.
Syntax
The syntax for overloading comparison operators is:
class_name operator==(const class_name& rhs);
class_name operator!=(const class_name& rhs);
class_name operator< (const class_name& rhs);
class_name operator> (const class_name& rhs);
class_name operator<=(const class_name& rhs);
class_name operator>=(const class_name& rhs);
Where,
class_name
: The name of the class where the operator is overloaded.rhs
: The right-hand side object to be compared.
Each of these functions should return a boolean (bool
) value.
How to use it
To overload comparison operators, follow these steps:
- Define your class.
- Within the class definition, declare and define the overloaded comparison operators.
- In each overloaded operator, implement the logic necessary to compare two objects of your class.
Program code snippet example
#include <iostream>
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
// Overloading == operator
bool operator==(const Point &rhs) const {
return x == rhs.x && y == rhs.y;
}
// Overloading != operator
bool operator!=(const Point &rhs) const {
return !(*this == rhs);
}
// Overloading < operator
bool operator<(const Point &rhs) const {
if(x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
// Overloading > operator
bool operator>(const Point &rhs) const {
return rhs < *this;
}
// Overloading <= operator
bool operator<=(const Point &rhs) const {
return !(rhs < *this);
}
// Overloading >= operator
bool operator>=(const Point &rhs) const {
return !(*this < rhs);
}
};
int main() {
Point p1(10, 20), p2(10, 30);
if(p1 == p2) std::cout << "Points are equal\n";
if(p1 != p2) std::cout << "Points are not equal\n";
if(p1 < p2) std::cout << "Point1 is less than Point2\n";
if(p1 > p2) std::cout << "Point1 is greater than Point2\n";
if(p1 <= p2) std::cout << "Point1 is less than or equal to Point2\n";
if(p1 >= p2) std::cout << "Point1 is greater than or equal to Point2\n";
return 0;
}
Output:
Points are not equal
Point1 is less than Point2
Point1 is less than or equal to Point2
Important to know
- Overloading comparison operators allows you to compare objects of user-defined types in a manner that suits the logic of your program.
- For complex classes, the implementation of these comparison operators may be non-trivial and should be handled with care to ensure accurate and expected comparisons.
- Note that
!=
,>
,<=
and `>=
operators are defined in terms of
==and
<`. This is done to reduce code redundancy and possible errors.
Best practices
When overloading comparison operators, it's important to consider the following:
Consistency: Ensure that the behavior of your overloaded operators is consistent with the behavior of the built-in types. This can make the program easier to understand and debug.
Symmetry: If
a == b
is true thenb == a
should also be true, same goes for the!=
operator. For<
and>
, ifa < b
is true thena > b
should be false.Transitivity: If
a == b
andb == c
thena == c
should be true. This also applies for<
and>
operators.Implementing Ordering: When implementing
<
,>
,<=
, and>=
, remember to establish a clear ordering between your objects. This is crucial when your objects are to be sorted or used in data structures that require ordering.
Overload the binary not equal operator for the Integer
class.