In one of our previous tutorials of C++, we have explained the concept of overloaded functions. C++ allows us to not only
overload the functions but also allows us to overload the operators, such as unary and binary operators.
Unary operators work on a single operand, whereas a binary operator work on two operands. Up next, we are going to explain how to overload some unary operators.
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 using 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 negate a value of built-in data type int variable by just using the unary minus - operator
with it.
int a = 10;
a = -a;
And, C++ uses the concept of operator
overloading which allows us to use unary minus - operator with an object of user-defined data-type to negate its value, just like we could do with built-in data-type variables.
ob1 = -ob1;
where, ob1 is an object of a class and unary minus operator - is overloaded in a way that we could just simply apply the unary minus - operator on the objects of class, in the same manner
as we apply unary minus - operator on any variables of built-in data-types to negate its value.
Without the concept of operator overloading, you wouldn't be able to work with the objects of a user-defined data type, the way you would work with built-in type variables and unary or binary operators.
How to achieve unary operator overloading?
Unary operator overloading can be achieved through an operator overloading member function.
An overloaded operator member function should be declared in the public section of a class.
Note: When redefining the meaning of an operator by operator overloading function, we cannot change its basic meaning.
For example, we cannot redefine minus operator -
to divide two operands of a user-defined data-type.
Overloading unary minus operator -
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.
But can we even negate the value in an object by using the unary minus operator with it?
Yes, we can! Let us see how to overload the minus operator -
// C++ Overloading unary minus(-) operator
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
void operator -();
};
//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 A :: operator -()
{
a = -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;
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 function - is : -10
Program Analysis
As you can see in the last program, we have created an operator overloaded member 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.
Advertisement
Overloading unary NOT operator !
As you know, the NOT ! operator when applied to any built-in type variable such as int will inverse its value from 0 to 1 or from 1 to 0 to represent a boolean value.
But can we even negate the value in an object by using the unary NOT ! operator?
Yes, we can! Let us see how to overload the NOT ! operator -
// C++ Overloading unary NOT(!) operator
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_a();
void get_a();
void operator !();
};
//Definition of set_a() function
void A :: set_a()
{
a = 0;
}
//Definition of get_a() function
void A :: get_a()
{
cout<< a <<"\n";
}
//Definition of overloading unary NOT operator ! function
void A :: operator !()
{
a = !a;
}
int main()
{
A ob;
ob.set_a();
cout<<"The value of a is : ";
ob.get_a();
//Calling operator overloaded function ! to inverse the value
!ob;
cout<<"The value of a after calling operator overloading function ! is : ";
ob.get_a();
}
Output
The value of a is : 0
The value of a after calling operator overloading function ! is : 1
Program Analysis
As you can see in the last program, we have created an operator overloaded member function to successfully overload the meaning of NOT ! operator, in such a way
that it could inverse the value of user-defined data type variables and not just built-in data type variables.
In the next article, we are going to explain how to apply operator overloading on binary operators.