- In my previous article, I had discussed about how private members of a class can be shared and accessed using “Friend Function”. In this article I am going to show you one another important features of C++ i.e Operator Overloading.
- Recall that, I have already explained how a function can be overloaded to implement function polymorphism in my post “Function Overloading” and similar to normal function how a constructor can be overloaded with the same mechanism in the post titled “Constructor Overloading”. If you haven’t read yet, I strongly recommend to read that posts first as it will help you in understanding the concept a lot.
- C++ has the ability to use operators with user-defined data type just like basic data type. C++ provides the special meaning to the operators.
- By operator overloading we can provide the new definition to operator. In other words, an operator can be used to perform different task besides its traditional purpose by overloading it. A general definition of operator overloading can be given as below:
“Operator overloading is a technique to provide special meaning to an operator.”
- Fore example if a screw driver can be used to unlock the door just like the key then a screw driver is said to be overloaded as we have used it for different purpose than it is designed for.
Rules For Overloading Operator
- Only existing operator in C++ can be overloaded. We cannot create our new operator for overloading.
- The overloaded operator must have at lease one operand that is user-defined type.
-
We can overload all C++ operators except the following ones.
- Class member access operator ( . and .* )
- Scope resolution operator ( :: )
- Size operator ( sizeof )
- Conditional operator ( ?: )
- We must remember that the precedence of operators cannot be changed by operator overloading. g. Multiplication operator has higher precedence than addition operator. We cannot change this meaning.
- We cannot use friend function to overload following operators. we can use only member function.
Operator Sign | Operator Name |
= | Assignment operator |
( ) | Function call operator |
[ ] | Subscript or index operator |
– > | Class member access operator |
- While overloading unary operator by using the friend function we have to pass one argument.
- While overloading unary operator by using the member function we are not passing any argument to the operator function.
- While overloading binary operator by using the friend function we are passing two arguments.
- While overloading binary operator by using the member function we are passing only one argument.
- Binary arithmetic operators such as +, -, *, and / must return a value.
How to overload operator?
- To overload an operator we are using special operator function. The format of operator function is as follow:
return-type operator op ( argument1, argument2, … )
;
- Where, return-type specifies return type of the function, operator is the keyword and op is the operator that we wish to overload.
- Operator function can be implemented either by using member function or with the help of friend function.
- A friend function has only one argument for unary operator and two arguments for binary operator. A member function does not take argument for unary operator but takes one argument for binary operation
- Operator function can be called by the following two ways:
-
Using Traditional Method
- This method is similar to call function which we are using for the other normal function. For unary operation we can call the operator function by:
object-name . operator op(operand); // where op=operator
- For binary operation we can call the operator function by:
object-name . operator op(operand1, operand2);
-
Using Expression Syntax
- This method is just like the normal expression we use for various arithmetic operations such as addition, subtraction, multiplication, etc. (e.g. a + b)
- For unary operation we can call the operator function.
op operand // here op = operator
or
operand op
- For binary operation we can call the operator function by:
operand1 op operator2