In our previous article of C++, we introduced you to the concept of operator overloading
and how it works with unary operators that just work on a single operand.
In this article, we are going to explain how to overload the binary operators, that work on two operands.
What is operator overloading and why we need it?
C++ has tried to bridge-in the gap between the user-defined data-type variables and built-in data-type variables, by
allowing us to work on both type of variables in a same way by the concept of operator overloading.
Operator overloading is to provide a special meaning or redefinition to an operator(unary or binary), so that it could work on user-defined data-type objects
just in the same way as it works on built-in data-type type variables.
For example, we could add two built-in data type int variables by just using the + operator between these two
int variables.
int a = 10, b = 20;
int result = a + b;
And, C++ also allows us to use + operator on two objects of user-defined data-type by using the concept of operator
overloading.
ob3 = ob1 + ob2;
where, ob1, ob2 and ob3 are the objects of a class and operator + is overloaded in a way that we could just simple apply the + operator on the objects of class, in the same manner
as we apply the + operator on any variables of built-in data-types.
Without the concept of operator overloading you wouldn't be able to add two objects of a user-defined data type the same way
as you add two built-in type variables.
How to achieve binary operator overloading?
Binary operator overloading can be achieved in two ways -
By an operator overloading member function, or
By an operator overloading non-memberfriend function.
Important points about binary operator overloading
An overloaded operator function should be declared in the public section of a class.
An operator overloaded function must have at least one user-defined data-type operand to work with.
When an operator overloaded function is a member function, it takes one operand of user-defined data type.
When an operator overloaded function is a friend function,
it takes two operands of user-defined data type.
Note: When redefining the meaning of an operator by operator overloading function, we cannot change its basic meaning.
For example, we cannot redefine + operator to divide two operands of user-defined data-type.
Overloading binary operator +
As you know, the + operator when applied to any two built-in type variables such as int, float, double, long will add their values up.
But can we even add the values in an object by using the binary + operator?
Yes, we can! Let us see how to overload the + operator.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
A operator +(A); //Binary operator + overloaded function, which takes an object of A and returns an object of A type.
};
//Definition of set_a() function
void A :: set_a()
{
a = 10;
}
//Definition of get_a() function
void A :: get_a()
{
cout<< a <<"\n";
}
//Definition of overloaded binary operator + function, which adds two objects of A and returns an object of A
A A :: operator +(A ob)
{
A temp;
temp.a = a = a + ob.a;
return temp;
}
int main()
{
A ob1, ob2;
ob1.set_a();
ob2.set_a();
//Calling operator overloaded function + to add two operands, objects
A ob3 = ob1 + ob2;
cout<<"The value of a after calling operator overloading function + is : ";
ob3.get_a();
}
Output
The value of a after calling operator overloading function - is : 20
Program Analysis
As you can see in the last program, we have created an operator overloaded member function to successfully overload/redefine the meaning of binary + operator, in such a way
that it could add the values of two user-defined data type objects in a same format as it adds the two built-in data type variables.
An important point to understand:
A ob3 = ob1 + ob2;
This line of code calls the operator * overloaded function and object ob1 calls the operator * overloaded function and ob2
is passed to the function as an argument.
Advertisement
Overloading binary operator *
As you know, the * operator when applied to any two built-in type variables such as int, float, double, long will multiply their values.
But can we even multiple the values in an object by using the binary * operator?
Yes, we can! Let us see how to overload the * operator.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
A operator *(A); //Binary operator +* overloaded function, which takes an object of A and returns an object of A type.
};
//Definition of set_a() function
void A :: set_a()
{
a = 5;
}
//Definition of get_a() function
void A :: get_a()
{
cout<< a <<"\n";
}
//Definition of overloaded binary operator * function, which multiples two objects of A and returns an object of A
A A :: operator *(A ob)
{
A temp;
temp.a = a = a * ob.a;
return temp;
}
int main()
{
A ob1, ob2;
ob1.set_a();
ob2.set_a();
//Calling operator overloaded function + to multiple two operands, objects
A ob3 = ob1 * ob2; //ob1 calls the operator * overloaded function and ob2 is passed as an argument.
cout<<"The value of a after calling operator overloading function * is : ";
ob3.get_a();
}
Output
The value of a after calling operator overloading function * is : 25
Program Analysis
As you can see in the last program, we have created an operator overloaded member function to
successfully overload the meaning of binary * operator, in such a way
that it could multiple the values of user-defined data type objects in a same format as it multiples the two built-in data type variables.
In the next article, we are going to explain how to apply operator overloading with friend function.