Method Syntax:
Return_type operator operator_symbol()
The method for an overloaded operator is defined a little bit differently. It is a return-type method that contains the word operator with the operator_symbol that we want to overload.
Overload the Comparison Operator
There are six assignment operators and all six can be overloaded. These are called comparison operators because they compare two values or variables (that stores the values) with each other. When the condition is satisfied, the next operation is performed. It is very useful to overload the operators when we have to compare the values with each other based on different conditions. The six comparison operators with the functionalities are explained in the following discussion.
1. < – The less than operator compares the following condition: if the first integer is less than the other, execute the body.
2. > – The greater than operator compares the following condition: if the first integer is greater than the second, execute the body.
3. <= – The less than or equal to operator compares the following condition: if the first term is less or equal to the second, execute the body.
4. >= – The greater than or equal to operator compares the following condition: if the first term is greater or equal to the second, execute the body.
5. == – The double equals checks whether the two integers are equal or not. If yes, execute the body. Otherwise, move to the next statement.
6. != – The not equal operator evaluates if the two terms are not equal. If yes, execute the body. Otherwise, the condition turns false and the next statement is executed.
We compare all the values using all the six comparison operators in pairs: < and and >=, == and !=.
Overload the < and <= Comparison Operators
Let’s compare the two values using the “<” and “<=” operators. Then, overload these operators.
Code:
using namespace std;
class Less
{
private :
int number;
public :
void input_0()
{ cin>>number; }
int operator < (Less x)
{
if(number<x.number)
return 1;
else
return 0;
}
int operator <= (Less x)
{
if(number<=x.number)
return 1;
else
return 0;
}
};
int main()
{
Less n1,n2;
cout<<"Please enter 1st number. ";
n1. input_0();
cout<<"Please enter 2nd number. ";
n2. input_0();
if(n1<n2)
{ cout<<"n1 is less than n2. "; }
else if(n1<=n2)
{ cout<<"n1 is less than or equal to n2. ";}
else
{ cout<<"n1 is not less than or equal to n2. "; }
return 0;
}
In this code, incorporate the <iostream> library and namespace std. Define a “Less” class because in this example, we overload the < (less than) and <= (less than or equal to) operators. In the “Less” class, declare a “number” data member. This data member has an integer type. Then, define an input_0() method to acquire the values from the user. Inside the body of this function, employ the “cin>>” so that the user can insert the number. Now, overload the less than (<) operator by defining the integer return type function as int operator <(). Within this function, specify an argument of the “Less” class. Next, apply the “if” condition. If the inserted number is less than “x.number”, return 1. Otherwise, return zero.
Overload the less than or equal to (<=) operator by implementing the same steps that we used to overload the (<) operator. Apply the main() function. Inside this, create two objects – “n1” and “n2” – of the “Less” class. Furthermore, use “cout<<” to print a message and call the input_0() method to get the values. Again, execute the “cout<<” and call the same method for object “n2”. Now, compare the first and second numbers. If n1 < n2, execute the body of “if”. And if n1<= n2, execute the body of the “else if”. Otherwise, execute the “else”. This way, we overload the operators.
Output:
Please enter 2nd number. 223
n1 is less than n2.
Overload the > and >= Comparison Operators
Let’s discuss the overloading of “>” and “>=” operators by running the C++ program.
Code:
using namespace std;
class Greater
{
private :
int number;
public :
void input_0()
{ cin>>number; }
int operator > (Greater x)
{
if(number > x.number)
return 1;
else
return 0;
}
int operator >= (Greater x)
{
if(number >= x.number)
return 1;
else
return 0;
}
};
int main()
{
Greater n1,n2;
cout<<"Please Enter 1st number. ";
n1. input_0();
cout<<"Please Enter 2nd number. ";
n2. input_0();
if(n1>n2)
{ cout<<"n1 is greater than n2. "; }
else if(n1>=n2)
{ cout<<"n1 is greater than or equal to n2. ";}
else
{ cout<<"n1 is not greater than or equal to n2. "; }
return 0;
}
At the start, type #include<iostream> and “using namespace std”. After this, create a “Greater” class since we overload the > (greater than) and >= (greater than or equal to) operators in this code. Define a “number” data member of the class and set this data member as private. Then, create an input_0() method to obtain the user-provided values. This function is set to be public. Now, the user can mention the number using the “cin>>” command. By calling the integer return type method and passing an argument of the “Greater” class to this method, we can overload the greater than (>) operator. Use the “if” condition to return 1 if the “number” is greater than “x.number”. Otherwise, return 0.
Repeat this procedure to overload the (>=) operator. After calling the main() method, create the objects of the “Greater” class. Execute the “cout<<” statement and input_0() function for both objects of the class. Here, compare the input values. If the first condition is satisfied, the “if” body is implemented. If the second condition is satisfied, the “else-if” body is implemented. If no condition is satisfied, execute the “else” part.
Output:
Please Enter 2nd number. 789
n1 is not greater than or equal to n2.
Overload the == and != Comparison Operators
Implement the code to check whether two values are equal or not using == and !=. Also, show how these operators are overloaded.
Code:
using namespace std;
class Equal
{
private :
int number;
public :
void Get_input()
{ cin>>number; }
int operator == (Equal x)
{
if(number==x.number)
return 1;
else
return 0;
}
int operator != (Equal x)
{
if(number!=x.number)
return 1;
else
return 0;
}
};
int main()
{
Equal n1,n2;
cout<<"Please enter 1st number. ";
n1.Get_input();
cout<<"Please enter 2nd number. ";
n2.Get_input();
if(n1==n2)
{ cout<<"n1 is equal to n2. ";}
else if(n1!=n2)
{ cout<<"n1 is not equal to n2. ";}
else
{ cout<<"Sorry "; }
return 0;
}
In the first statement, incorporate the <iostream> library. Along with this, use the “namespace std”. An “Equal” class is created to overload the = (equal) and != (not equal) operators. Define a “number” variable having an int type, and define the Get_input() method. Now, use “cin>>” to get the integer from the user. After this, overload the equal (=) and not equal (!=) operators by employing the functions of the integer type and set the overloaded operator to == and != in the body of these functions. Additionally, use the main() method to create the objects of the “Equal” class. Get the typed values of the user by calling the Get_input() function twice. Compare these values to check whether they are equal or not equal. We don’t need to overload both equal and not equal operators because if the values are equal, an overloaded equal operator is executed. If not, the “else if” will display a message on the terminal. But, to explain the working of both methods here, we define the two overloaded methods.
Output:
Please enter 2nd number. 45
n1 is equal to n2.
Conclusion
This guide shows an information about what the comparison operators are and how we can overload them in C++. Overloading the comparison operator is so easy and simple, it is just like overloading a method. But here, we mention the overloaded operator name with the keyword “operator”. All the comparison operators are overloaded in the codes of this guide.