Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Binary Operator Overloading in C++


1. Introduction to Binary Operator Overloading

The operator operates on the operands. Say, for example, 3 + 5 = 8. Here, one can say 3 and 5 are operands. Moreover, the + is the operator which denotes the Addition Operation on the operands 3 and 5. Eight results from the operation. As ‘operator +’ operates on two operands, we call this + operator as a Binary Operator. The C++ programming language knows how to add two standard variables. But, how about the class objects? The language does not know how to add two class objects as it does not know which internal data members to pick for the Addition Operation. In that case, we should give more responsibility to the operator + to tell how it can add two specific class objects. Now, we will end up into Binary Operator Overloading. With all that said, in this example, we will explore how to overload a ‘Binary Operator +’.

2. The ‘DivBy5’ Class to Overload + Operator

Have a look at the below-shown class diagram for the DivBy5 class:

The DivBy5 C++ Class Layout

The DivBy5 C++ Class Layout

From the above diagram, one may perceive what this class object will do. Moreover, the plus operator supported by the C++ language does not know how to add the two instances of the DivBy5 class. Say for Example, when we pass 7 to the constructor of this DivBy5 class, C++ initializes the members as shown below:

Members of DivBy5 Class

Members of DivBy5 Class

if we want to add two instances of the above-specified object, as a person we know that only the member m_theNumber should be added and other members should be calculated. Now, we should educate C++ how it should add two DivBy5 objects. As a result, the statement Obj1 + Obj2 become valid for C++. Note, both the objects must be instances of DivBy5 class.

At present the DivBy5 class looks like the below one:


Snippet 01: Acts as a default constructor as well as a constructor with a single argument. Note that this constructor accepts a single integer parameter. Because of this, the statement ‘ DivBy5 = 7;’ is viable. It invokes the constructor by passing the value 7 to it. In addition, The constructor fills all the data members of the class.

Snippet 02: Performs printing of the constructed results. It shows the result of the ‘division by five’ for the passed-in number.

4. Overloading ‘+’ Operator Using Member Function

The above class does not know how to add two instances. Look at the below snippet:

The third statement will give the Compiler Error. This is because, it does not know how to add obj1 and obj2 using the operator +. The below code is added to the class DivBy5 to overload the plus operator, so it knows how to add two DivBy5 instances. Code is below:

In the above snippet, the first operand invokes the ‘ operator +’ overload and takes second operand as a parameter to it. Note, how do we add the two numbers and return the result. It is tricky. The return type expected is DivBy5 and what we return is an integer. However, the return result will invoke the constructor with a single integer as an argument and it is returned to the caller as a result of the binary operation add. The complete code is below:


Usage 01: We create two objects obj1 and obj2 and print the objects after the creation. The print gives the result of the divisions 17/5 and 22/5.

Usage 02: Here we add two objects using our overloaded + operator. It operates on two objects obj1 and obj2 and returns the result as a third object obj3. Note that the obj1 calls our overloaded operator by passing the obj2 as a parameter. With this in mind, we can look at the plus operator implementation once again. The m_theNumber within the function refers the first operand of the plus operator. In our case, it is the member of obj1. However, the second operand is referred as ‘ obj2nd.m_theNumber’ and is passed as a parameter to overloaded operator function. We return the added value of 39 back to the caller. We get the result back in the form of DivBy5 in Obj3 because of the Conversion Constructor.

Usage 03: Here we are trying to add the obj1 with the integer value of 3. The value three invokes the conversion constructor and then passed as a parameter to the overloaded binary operator +. The return value is stored in the obj4. As told in the code snippet for ‘usage 02’, obj1 makes call to the overloaded plus operator.

The below picture shows the result of the above code:

Binary Operator Overloading Program Example Output 1

Binary Operator Overloading Program Example Output 1

What happens if we perform the addition like DivBy5 Obj4 = 3 + obj1 ?

We get a compiler error as 3 is not an object. Because, it is not possible to invoke the overloaded + operator with an integer type. In this situation, we should use the Global Friend Function to overload the plus operator.

5. Overloading using Friend Function

When we use global friend function, to overload a binary operator, we pass both objects as arguments to the function. So we no need to worry that the first operand should be an object. Even if it is an integer value, it will be converted as an object. Below is the full code:


Sample 04: Here, we declared the Overloaded plus operator as a Friend Function. Moreover, we pass both the operands as the parameters to it. Since, this overload is a friend to the class, it can access all the private members of the passed in class object.

Sample 05: Here, we overload the ‘Operator +’ using Global Function. Unlike member function, it picks up both the operands as an argument. The return value will go through a Conversion Constructor. As a result, the caller gets DivBy5 object as the result of the Binary Operation.

Usage 04: In the previous section, we saw that the first operator should not be a constant value. But overloading the Plus operator as a Friend Function makes the C++ statement ‘ 3 + obj1’ possible. Because, here the value 3 is passed as an argument and a Conversion Constructor converts that as an object of first parameter type. The below picture shows the result of program execution:

Program Output 2: Binary Operator Overloading

Program Output 2: Binary Operator Overloading

Categories: C++

Tags: ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.