Overloading Relational Operators in C++
Last Updated :
29 Jan, 2024
In C++, operator overloading is used to redefine the behavior of already existing operators. Similarly, overloading the relational operators is commonly used to compare the instances of user-defined classes. By overloading these operators we can easily define the behavior of comparisons for the objects of a given class.
In this article, we will see how to overload relational operators for a class in C++.
Relational Operator Overloading
Overloading a relational operator is similar to any other operator overloading. We can easily implement it by overloading "=="
operator and "<" operator
as member functions, and other relational operators can be derived from these.
C++ Program to Implement Relational Operator Overloading
The below example demonstrates how the overloading of relational operators is done in C++.
C++
// C++ program to demonstrate how to overload relational
// operators.
#include <iostream>
using namespace std;
class MyClass {
private:
int value; // Private member to store the value
public:
// Constructor to initialize MyClass objects
MyClass(int val)
: value(val)
{
}
// Overloading the equality operator (==)
bool operator==(const MyClass& other) const
{
// Compare the value of this object with the value
// of 'other'
return value == other.value;
}
// Overloading the inequality operator (!=)
bool operator!=(const MyClass& other) const
{
// Utilize the already overloaded '==' operator
return !(*this == other);
}
// Overloading the less than operator (<)
bool operator<(const MyClass& other) const
{
// Compare the value of this object with 'other' for
// less than
return value < other.value;
}
// Overloading the greater than operator (>)
bool operator>(const MyClass& other) const
{
// Compare the value of this object with 'other' for
// greater than
return value > other.value;
}
// Overloading the less than or equal to operator (<=)
bool operator<=(const MyClass& other) const
{
// Utilize the already overloaded '>' operator
return !(*this > other);
}
// Overloading the greater than or equal to operator
// (>=)
bool operator>=(const MyClass& other) const
{
// Utilize the already overloaded '<' operator
return !(*this < other);
}
};
int main()
{
MyClass obj1(20);
MyClass obj2(20);
// Using overloaded relational operators
if (obj1 == obj2) {
cout << "obj1 is equal to obj2" << endl;
}
else {
cout << "obj1 is not equal to obj2" << endl;
}
if (obj1 < obj2) {
cout << "obj1 is less than obj2" << endl;
}
else {
cout << "obj1 is not less than obj2" << endl;
}
// Using overloaded '!=' operator
if (obj1 != obj2) {
cout << "obj1 is not equal to obj2" << endl;
}
else {
cout << "obj1 is equal to obj2" << endl;
}
// Using overloaded '>' operator
if (obj1 > obj2) {
cout << "obj1 is greater than obj2" << endl;
}
else {
cout << "obj1 is not greater than obj2" << endl;
}
// Using overloaded '<=' operator
if (obj1 <= obj2) {
cout << "obj1 is less than or equal to obj2"
<< endl;
}
else {
cout << "obj1 is not less than or equal to obj2"
<< endl;
}
// Using overloaded '>=' operator
if (obj1 >= obj2) {
cout << "obj1 is greater than or equal to obj2"
<< endl;
}
else {
cout << "obj1 is not greater than or equal to obj2"
<< endl;
}
return 0;
}
Outputobj1 is equal to obj2
obj1 is not less than obj2
obj1 is equal to obj2
obj1 is not greater than obj2
obj1 is less than or equal to obj2
obj1 is greater than or equal to obj2
Explanation: In the above program the overloading of all the relational operators (==
, !=
, <
, >
, <=
, >=
) available in C++ is done for a custom class named MyClass
. It shows that how we can compare instances of user defined class with the help of
these operators in a manner way like using any primitive data types, which store an integer value.
Similar Reads
Overloading the Comma Operator In C++, we can overload the comma operator using Operator Overloading. For Example: For "Send the query X to the server Y and put the result in variable Z", the "and" plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expr
5 min read
How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo
2 min read
Relational Operators on STL Array in C++ The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b
3 min read
How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op
2 min read
Overloading of function-call operator in C++ In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by â()â which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo
3 min read
How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi
2 min read