In our previous articles of C++, we introduced you to the concept of operator overloading
and how we could perform operator overloading using the member function.
In this article, we are going to explain how to perform operator overloading using the non-memberfriend function.
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 using member function or a non-memberfriend function
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 operator overloading?
Operator overloading can be achieved in two ways -
By an operator overloading non-memberfriend function.
Important points about operator overloading using friend function
An overloaded operator friend could be declared in either private or public section of a class.
When an operator overloaded function is a friend function,
it takes two operands of user-defined data type.
When redefining the meaning of an operator by operator overloading friend function, we cannot change its basic meaning.
For example, we cannot redefine minus operator -
to divide two operands of user-defined data-type.
Overloading binary minus operator - using friend function
As you know, As you know, the minus operator - when applied to any built-in type variable such as int, float, double,
long will change its value from positive to negative.
We can even change the sign of values of an object by using the unary - operator using member function.
But let's see how to achieve the same using
a non-memberfriend function.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
friend A operator -(A); // Friend function which takes an object of A and return 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 unary minus operator - friend function
A operator -(A ob)
{
ob.a = -(ob.a);
return ob;
}
int main()
{
A ob;
ob.set_a();
cout<<"The value of a is : ";
ob.get_a();
//Calling operator overloaded function - to negate the value
ob = -ob; //ob object is passed as an argument to the friend function and its negative version is returned.
cout<<"The value of a after calling operator overloading friend function - is : ";
ob.get_a();
}
Output
The value of a is : 10
The value of a after calling operator overloading friend function - is : -10
Program Analysis
As you can see in the last program, we have created an operator overloaded non-memberfriend function to successfully overload
the meaning of unary minus - operator, in such a way
that it could negate the value of user-defined data type variables and not just built-in data type variables.
An important point to understand:
ob = -ob;
This line of code calls the operator - overloaded friend function and copy of the object ob is passed to the function as an argument
i.e. object ob is passed to the friend function as a copy and not as the reference, that's why the changes made to the object ob in this friend function won't be reflected.
Hence, the operator overloaded friend function after negating the value of object, assigns the resulting value to a new object of A class which is returned by the function and gets
assigned to the original object ob.
Advertisement
Overloading binary minus operator - using pointer and friend function
In the last example, you saw how we used a friend function to perform operator overloading, which passed an object by value to the friend function.
Now, let's see how we can perform operator overloading
by non-member friend function using pointers.
This would allow us to pass the object by reference to the friend function and make permanent changes to the passed object.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
friend void operator -(A &);
};
//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 unary minus operator - function
void operator -(A & ob)
{
ob.a = -(ob.a);
}
int main()
{
A ob;
ob.set_a();
cout<<"The value of a is : ";
ob.get_a();
//Calling operator overloaded function - to negate the value
-ob; //ob object is passed as an argument to the friend function.
cout<<"The value of a after calling operator overloading function - is : ";
ob.get_a();
}
Output
The value of a is : 10
The value of a after calling operator overloading friend function - is : -10
Program Analysis
As you can see in the last program, we have created an operator overloaded non-memberfriend function to successfully overload
the meaning of unary minus - operator, in such a way
that it could negate the value of user-defined data type variables and not just built-in data type variables.
An important point to understand:
ob = -ob;
This line of code calls the operator - overloaded friend function and the object ob1 is passed to the function as an argument. This operator overloaded friend function
negate its value and returns the object.
Overloading binary operator * using friend function
As you know, the * operator when applied to any two built-in type variables such as int, float, double, long will multiply their values.
And, we can even multiple the values in an object by using the binary * operator using member function. But let's see how to achieve the same using
a non-memberfriend function.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
friend A operator *(A,A); //Binary operator * overloaded friend function, which takes two 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 * friend function, which multiples two objects of A and returns an object of A
A operator *(A ob1, A ob2)
{
A temp;
temp.a = ob1.a * ob2.a;
return temp;
}
int main()
{
A ob1, ob2;
ob1.set_a();
ob2.set_a();
cout<<"The value of a in first object : ";
ob1.get_a();
cout<<"The value of a in second object : ";
ob2.get_a();
//Calling operator overloaded friend function + to multiple two operands, objects
A ob3 = ob1 * ob2; //ob1 and ob2 objects are passed as an argument to the operator * overloaded friend function.
cout<<"The value of a after calling operator overloading function * is : ";
ob3.get_a();
}
Output
The value of a in first object : 5
The value of a in second object : 5
The value of a in third object after calling operator overloading function * is : 25
Program Analysis
As you can see in this program, we have created an operator overloaded non-memberfriend function
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.
An important point to understand :
A ob3 = ob1 * ob2;
This line of code calls the non-member operator * overloaded friend function and the objects ob1 and ob2 are passed
to this function as arguments to it, which returns an object, containing the resulting value of multiplication of two objects.