What is Binary Operator Overloading in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

To understand Binary Operator Overloading, we should focus on two separate terms it is made up of:

  1. Binary Operator: It operates on two operands to give a result. There are three binary operators: mathematical, logical, and relational.
  2. Operator Overloading: An operator can be used with one or two operands. When we use an operator and give it a special meaning without changing its original property, we call it operator overloading.

Since we have two kinds of operators: unary and binary, there are two types of operator overloading. This article is solely for binary operator overloading.

What is Binary Operator Overloading in C++? When we overload an operator which works on two operands, it is known as binary operator overloading. It is used to manipulate the values of two objects of the same class. Now that we know about binary operator overloading let’s move on to understanding the syntax that will help us go deep into binary operator overloading in C++.

Syntax of the Binary Operator Overloading in C++

The basic syntax of binary operator overloading is as follows:

It is very similar to our function definition in C++. We start with a return data type of our function and follow it with the name of our class. The catch comes after this, instead of writing the function name, we initialize it with the operator we want to overload. One more thing to remember is that, unlike our default function, the binary operator overloaded function can only have one argument.

NOTE: Here, op replaces the operator we want to overload (+,-,/,*). The “operator” is the keyword necessary to use as a part of the syntax.

If we are defining our function inside the class, our syntax would be as follows:

It is not always a suggested method to define functions inside our class, but it is a point to note.

Binary Operator Overloading Algorithm

Below are the essential steps to stick to for writing a binary operator overloading program:

STEP 1: Start with initializing the class name.

STEP 2: Declare data members privately & member functions publicly.

STEP 3: Create binary operator overloaded functions as required. With this, our class definition ends.

3.1) Decide the operator you want to overload.

3.2) Use the syntax discussed above to initialize the function.

3.3) Create an object of the class where the result will be stored.

3.4) Complete the function body by manipulating the objects and returning them.

STEP 4: Start the driver code by initializing class objects.

STEP 5: Make sure to create a resultant object. It will store the manipulation done by the binary operator.

STEP 6: Print the resultant object using the output member function.

It is a basic step-by-step routine to follow up. The practical usage of this can be comprehended in the examples discussed below.

Examples

Now that we have looked at all the main details of binary operator overloading, it is time to use it practically. We will make use of two key examples that’ll make us clear the air even more.

Example 1: Program to Perform the Arithmetic Operation on Objects of a Class by Overloading Multiple Binary Operators

Let us start with the most basic kind of program. We take two numbers as members of a class. And we try to do multiple operations on them. For this example, let us choose two operators: addition (+) and division (/).

The class declaration is as follows:

Let us go on to define some common functions needed for input and output:

Now that we are halfway through, we are left with the definition of our binary operator overloaded functions. Overloading here has helped us in including the addition & division facility in our class. It is as follows:

The definition of both these functions is almost similar. The only difference is between the two operators we are using. With this, we mark the end of our class and function definitions.

The driver code is as follows:

It is the end of our program. Make sure to test it on your devices and get the desired result. We can see the depiction below when entering some test values in our program:

ADDITION AND DIVISION

Example 2: Program to Perform the Addition and Subtraction of Two Complex Numbers Using the Binary (+) and (-) Operator

We have got two complex numbers. And since a complex number has two parts: a real part and an imaginary one, we should consider making a class to ease our calculation. Hence, our complex number is a class here.

COMPLEX NUMBER REPRESENTATION

The class declaration is as follows:

Now that we are done with our class declaration, we should create some common functions to take input and display our results.

Our operator-overloaded functions look similar to the syntax we have defined before. Now that our class & function definition is over let us move on to the driver code:

Note: How we used an object (sum & sub) to store the result of the two other objects (c1 & c2). It’s similar way we use the same kind of data type while doing a particular manipulation. Pretty simple, right? Now that we have the whole program in front of us let us look at our outcome:

COMPLEX NUMBER PROGRAM OUTPUT

And we get the desired result. You can try with other test cases and play around.

Learn More

It is not the end of binary operator overloading. Make some good use of the concept & it’ll last long. There are similar concepts of operator overloading & unary operator overloading. Revise the foundation and related topics here.

Conclusion

  1. When a binary operator is given a special function to manipulate two operands, it is called binary operator overloading.
  2. We can only input one argument with it. It is necessary always to mention it.
  3. The argument can be the same object or a different one.
  4. There are two crucial examples discussed in this article.
  5. Make sure to visit here & get more information about operator overloading and unary operator overloading.