Operator Overloading in C++


The process of giving special meaning to the existing c++ operator is known as "Operator Overloading".
Using the concept of operator overloading we can use operators with object of the class.
For example: We can use + to perform addition of two integer or floating point numbers. In the same way we can overload + operator so that we can perform addition of two objects of the class.
When you overload the existing operator the basic syntax rules of the operator is not changed. It remains as it is.
In order to overload particular operator you need to use special function known as operator function.

     

How to define Operator Function


The general syntax for defining operator overloading is given below:
Return-type operator op (Argument List)
{

}
Here,
Return-type
indicates the type of the value that is return by the function.
Operator is the name of the operator function.
Op is the operator to which you want to overload
Argument List indicates number of arguments need to be passed.
The operator function can be either member function or friend function.

Unary Operator Overloading:


Unary operator having one operand so generally we need to pass one argument to the operator function.
If you define operator function as a member function then it will accept no argument. Because the object that is used to invoke the operator function is passed implicitly to the operator function.
If you define operator function as a friend function then it will accept one argument. Because friend functions is not a member function so it is not invoked using object of the class. Thus we need to pass object as an argument explicitly.
Once the operator function is defined you can invoke operator function in following different ways:
op ObjectName;
or
// For Member Function
ObjectName.operator ();
// For Friend Function
Operator op (ObjectName);

Binary Operator Overloading:


Binary operator having two operands so generally we need to pass two arguments to the operator function.
If you define operator function as a member function then it will accept one argument. Because the object that is used to invoke the operator function is passed implicitly to the operator function and other object is passed explicitly to the function.
If you define operator function as a friend function then it will accept two arguments. Because friend functions is not a member function so it is not invoked using object of the class. Thus we need to pass two objects as an argument explicitly.
Once the operator function is defined you can invoke operator function in following different ways:
ObjectName1 op ObjectName2;
Or
// For Member Function
ObjectName1.operator (ObjectName2);
// For Friend Function
Operator op (ObjectName1, ObjectName2);

Operator Overloading Rules


There are some rules or restrictions for overloading operators. These rules are as given below:
1. You can overload only existing operator. New operator can not be created.
2. you can not overload following operators:
Sizeof operator
Conditional operator (? :)
Scope resolution operator (::)
Class member access operator (., .*)
3. By overloading operator you can not change the syntax rules of operator.
4. You cannot use friend function to overload following operators:
= Assignment operator.
() Function call operator.
{} Subscripting operator.
-> Class member access operator.
5. When you overload unary operator using member function it will take no explicit argument. But if you overload unary operator using friend function then it will take one explicit argument.
6. When you overload binary operator using member function it will take one explicit argument. But if you overload binary operator using friend function then it will take two explicit arguments.
7. Overloaded operators cannot have default arguments.

Download Projects


Download Programs