Overloading Relational Operators in C++ Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 OverloadingOverloading 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 OverloadingThe 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. Comment More infoAdvertise with us Next Article Overloading Relational Operators in C++ A avinashw5if Follow Improve Article Tags : C++ Programs C++ cpp-operator-overloading C++-Operator Overloading Operator Overloading cpp-overloading CPP Examples +3 More Practice Tags : CPP 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 How to Overload the Multiplication Operator in C++? In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator 2 min read Why Assignment Operator Overloading Must Return Reference? Operator overloading in C++ allows us to define custom behaviors for operators when applied to user-defined types. One of the most commonly overloaded operators is the assignment operator (=), which is used to assign the value of one object to another. However, when overloading the assignment operat 4 min read What Are the Basic Rules and Idioms for Operator Overloading in C++? In C++, operator overloading is a form of compile-time polymorphism where an operator is redefined to provide a special meaning beyond its typical operation. It allows developers to use traditional operators with user-defined types (or classes). In this article, we will learn the basic rules and idi 3 min read Arrow Operator vs. Dot Operator in C++ In C++, we use the arrow operator (->) and the dot operator (.) to access members of classes, structures, and unions. Although they sound similar but they are used in different contexts and have distinct behaviours. In this article, we will learn the key differences between the arrow operator and 3 min read Like