Home » Binary Operator Overloading in C++

Binary Operator Overloading in C++

by Online Tutorials Library

Binary Operator Overloading in C++

This section will discuss the Binary Operator Overloading in the C++ programming language. An operator which contains two operands to perform a mathematical operation is called the Binary Operator Overloading. It is a polymorphic compile technique where a single operator can perform various functionalities by taking two operands from the programmer or user. There are multiple binary operators like +, -, *, /, etc., that can directly manipulate or overload the object of a class.

Binary Operator Overloading in C++

For example, suppose we have two numbers, 5 and 6; and overload the binary (+) operator. So, the binary (+) operator adds the numbers 5 and 6 and returns 11. Furthermore, we can also perform subtraction, multiplication, and division operation to use the binary operator for various calculations.

Syntax of the Binary Operator Overloading

Following is the Binary Operator Overloading syntax in the C++ Programming language.

Here,

return_type: It defines the return type of the function.

operator: It is a keyword of the function overloading.

binary_operator_symbol: It represents the binary operator symbol that overloads a function to perform the calculation.

arg: It defines the argument passed to the function.

Steps to Overload the Binary Operator to Get the Sum of Two Complex Numbers

Step 1: Start the program.

Step 2: Declare the class.

Step 3: Declare the variables and their member function.

Step 4: Take two numbers using the user-defined inp()function.

Step 6: Similarly, define the binary (-) operator to subtract two numbers.

Step 7: Call the print() function to display the entered numbers.

Step 8: Declare the class objects x1, y1, sum, and sub.

Step 9: Now call the print() function using the x1 and y1 objects.

Step 10: After that, get the object sum and sub result by adding and subtracting the objects using the ‘+’ and ‘-‘ operators.

Step 11: Finally, call the print() and print2() function using the x1, y1, sum, and sub.

Step 12: Display the addition and subtraction of the complex numbers.

Step 13: Stop or terminate the program.

Example 1: Program to perform the addition and subtraction of two complex numbers using the binary (+) and (-) operator

Let’s create a program to calculate the addition and subtraction of two complex numbers by overloading the ‘+’ and ‘-‘ binary operators in the C++ programming language.

Output

Input two complex numbers:  5  7  Input two complex numbers:  3  5  Entered values are:  5 + 7i  3 + 5i  The addition of two complex (real and imaginary) numbers: 8 + 12i  The subtraction of two complex (real and imaginary) numbers: 2 - 2i     

In the above program, we take two numbers from the user, and then use the binary operator to overload the ‘+’ and ‘-‘ operators to add and subtract two complex numbers in a class.

Example 2: Program to add two numbers using the binary operator overloading

Let’s create a program to calculate the sum of two numbers in a class by overloading the binary plus(+) operator in the C++ programming language.

Output

Enter the first number: 5  Enter the second number: 6  The sum of two numbers is: 11     

In the above program, we take two numbers, 5 and 6, from the user and then overload the binary plus (+) operator to perform the addition that returns the sum of two numbers is 11.

Example 3: Program to perform the arithmetic operation by overloading the multiple binary operators

Let’s create a program to overload multiple binary operators in a class to perform the arithmetic operations.

Output

Addition : 40  Subtraction : 0  Multiplication : 400  Division : 1      

In the above program, we declare the value of variable num is 20 and then overload the binary plus (+), minus (-), multiply (*), and division (/) operator to perform the various arithmetic operations in the Arith_num class.


You may also like