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
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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class DivBy5 { public: //Sample 01: It acts as Default Constructor as well as // Conversion constructor. DivBy5(int number = 0) { m_theNumber = number; m_quotient = m_theNumber/5; m_reminder = m_theNumber%5; } //Sample 02: Print the Reminder and divident void print() { cout<<"Quotient ="<<m_quotient<<endl; cout<<"Reminder ="<<m_reminder<<endl; } private: int m_quotient, m_reminder, m_theNumber; }; |
Explanation
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:
1 2 3 |
DivBy5 obj1(17); DivBy5 obj2(22); DivBy5 Obj3 = obj1 + obj2; |
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:
1 2 3 4 5 |
//Sample 03: OverLoading Operator + DivBy5 operator+(DivBy5 obj2nd) { return m_theNumber + obj2nd.m_theNumber; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// TestIt.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; //========================================================= //Example: Overloaded + Operator as class member function //========================================================= class DivBy5 { public: //Sample 01: It acts as Default Constructor as well as // Conversion constructor. DivBy5(int number = 0) { m_theNumber = number; m_quotient = m_theNumber/5; m_reminder = m_theNumber%5; } //Sample 02: Print the Reminder and divident void print() { cout<<"Quotient ="<<m_quotient<<endl; cout<<"Reminder ="<<m_reminder<<endl; } //Sample 03: OverLoading Operator + DivBy5 operator+(DivBy5 obj2nd) { return m_theNumber + obj2nd.m_theNumber; } private: int m_quotient, m_reminder, m_theNumber; }; void main() { //Usage 01: Construct 2 objects DivBy5 obj1(17); DivBy5 obj2(22); cout<<"Object 1"<<endl; obj1.print(); cout<<"Object 2"<<endl; obj2.print(); //Usage 02: Use the + Operator DivBy5 Obj3 = obj1 + obj2; cout<<"Object 3"<<endl; Obj3.print(); //Usage 03: Use the + Operator with Const DivBy5 Obj4 = obj1 + 3; cout<<"Object 4"<<endl; Obj4.print(); } |
Explanation
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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// TestIt.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; //========================================================= //Example: Overloaded + Operator as class member function //========================================================= class DivBy5 { public: //Sample 01: It acts as Default Constructor as well as // Conversion constructor. DivBy5(int number = 0) { m_theNumber = number; m_quotient = m_theNumber/5; m_reminder = m_theNumber%5; } //Sample 02: Print the Reminder and divident void print() { cout<<"Quotient ="<<m_quotient<<endl; cout<<"Reminder ="<<m_reminder<<endl; } ////Sample 03: OverLoading Operator + //DivBy5 operator+(DivBy5 obj2nd) //{ // return m_theNumber + obj2nd.m_theNumber; //} //Sample 04: Overloading Operator using the Friend friend DivBy5 operator+(DivBy5 Obj1, DivBy5 Obj2); private: int m_quotient, int m_reminder, int m_theNumber; }; //Sample 05: Overloaded operator implementation DivBy5 operator+(DivBy5 Obj1, DivBy5 Obj2) { return (Obj1.m_theNumber + Obj2.m_theNumber); } void main() { //Usage 01: Construct 2 objects DivBy5 obj1(17); DivBy5 obj2(22); cout<<"Object 1"<<endl; obj1.print(); cout<<"Object 2"<<endl; obj2.print(); //Usage 02: Use the + Operator DivBy5 Obj3 = obj1 + obj2; cout<<"Object 3"<<endl; Obj3.print(); //Usage 03: Use the + Operator with Const DivBy5 Obj4 = obj1 + 3; cout<<"Object 4"<<endl; Obj4.print(); //Usage 04: Use the + Operator with Const (2nd way) DivBy5 Obj5 = 3 + obj1; cout<<"Object 5"<<endl; Obj4.print(); } |
Explanation
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
Categories: C++
Tags: Binary Operator Overloading, Operator Overloading