C++

C++ Overload Comparison Operator in

Operator overloading is a crucial concept in C++ that lets you achieve the functionality of the built-in operators while working with user-defined data types. Comparison operators in C++ are the ones that are there to compare two values with each other such as “==”, “!=”, “>”, “<”, “>=”, and “<=”. This article will share the methods of overloading all six of these comparison operators in C++ in Ubuntu 20.04.

Example # 1: Overloading the “==” Operator in C++:

For overloading the “==” operator in C++, we have designed the following C++ class:

The name of our C++ class is ProductPrice. Within this class, we have a private member “price”. After that, we have some public member functions. The first function, named “userInput,” takes the product price as input from the user. Then, we have the function for overloading the “==” operator in C++. We have passed an object of the ProductPrice class to this function. Within this function, we have an “if” statement that will return “true” if the prices of the two products are equal, otherwise “false”.

To test this, we have the driver function shown in the image below:

In our “main()” function, we have first created two objects of our class. Then, we have called the “userInput” function with each of the two objects one by one to take the prices of the two products as input from the user. After that, we have an “if” statement to print a message if the two prices are equal and another message if not.

We compiled this program using the following command:

$ g++ ComparisonOperator.cpp –o ComparisonOperator

Then, we executed it using the command shown below:

$ ./ComparisonOperator

Upon executing this code, we were asked to enter the price of the first product as shown in the following image:

Then, we were asked to enter the price of the second product as shown in the image below:

Since we entered the same prices for both products, we can see in the following output that our “==” operator has been overloaded successfully.

Now, to test it for the “else” statement, we entered two different product prices, as shown in the image below:

Example # 2: Overloading the “!=” Operator in C++:

For overloading the “!=” operator in C++, we have designed the following C++ class:

This program looks very much like our first example. The only difference is that we have replaced the “==” operator with the “!=” operator everywhere.

To test this, we have the driver function shown in the image below:

We have called the “userInput” function in our “main()” function with each of the two objects one by one to take the prices of the two products as input from the user. After that, we have an “if” statement to print a message if the two prices are not equal and another message if they are.

When we executed this code with two different product prices, we figured out that our inequality operator had been overloaded successfully, as you can see from the following image:

Now, to test it for the “else” statement, we entered two similar product prices, as shown in the image below:

Example # 3: Overloading the “>” Operator in C++:

For overloading the “>” operator in C++, we have designed the following C++ class:

This program is also similar to our examples above. The only difference is that we have replaced the “!=” operator with the “>” operator everywhere.

To test this, we have the driver function shown in the image below:

We have called the “userInput” function in our “main()” function with each of the two objects one by one to take the prices of the two products as input from the user. After that, we have an “if” statement to print a message if the first price is greater than the other and another message if not.

When we executed this code with two different product prices, we figured out that our greater than operator had been overloaded successfully, as you can see from the following image:

Now, to test it for the “else” statement, we entered two similar product prices, as shown in the image below:

Example # 4: Overloading the “<” Operator in C++:

For overloading the “<” operator in C++, we have designed the following C++ class:

This program is also similar to our examples above. The only difference is that we have replaced the “>” operator with the “<” operator everywhere.

To test this, we have the driver function shown in the image below:

We have called the “userInput” function in our “main()” function with each of the two objects one by one to take the prices of the two products as input from the user. After that, we have an “if” statement to print a message if the first price is less than the other and another message if not.

When we executed this code with two different product prices, we figured out that our less than operator had been overloaded successfully, as you can see from the following image:

Now, to test it for the “else” statement, we entered two different product prices again, but this time, the price of the first product is greater than the second, as shown in the image below:

Example # 5: Overloading the “>=” Operator in C++:

For overloading the “>=” operator in C++, we have designed the following C++ class:

This is slightly different from our examples above. While overloading the operator this time, we have two conditions within the “if” statement separated by the “||” symbol. It means that this statement will check if the price of a product is greater than or equal to another.

To test this, we have the driver function shown in the image below:

We have called the “userInput” function in our “main()” function with each of the two objects one by one to take the prices of the two products as input from the user. After that, we have an “if” statement to print a message if the first price is greater than or equal to the other and another message if not.

When we executed this code with two different product prices, we figured out that our greater than or equal to the operator has been overloaded successfully, as you can see from the following image:

Now, to test it for the “else” statement, we entered the first product price less than the second one, as shown in the image below:

Example # 6: Overloading the “<=” Operator in C++:

Following the same method as our fifth example, we also overloaded the “<=” operator, and its code is as follows:

The execution results of this program with different values are shown in the images below:

Conclusion:

This article explained very well how you could overload the six comparison operators in C++ in Ubuntu 20.04. We started with a brief introduction of operator overloading, then all the six comparison operators in C++ were overloaded. We have used the same user-defined class for demonstrating all of these methods. Once you go through these methods, you will be able to overload these comparison operators for any other user-defined class.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.