Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Operator overloading

Operator overloading is an important concept in C++. Overloaded


operator is used to perform operation on user-defined data type.
For example '+' operator can be overloaded to perform addition on
various data types, like for Integer, String(concatenation) etc.

Almost any operator can be overloaded in C++. However there are few
operator which can not be overloaded.

Operator that are not overloaded are follows

 scope operator - ::
 sizeof

 member selector - .

 member pointer selector - *

 ternary operator - ?:

Operator Overloading Syntax


Rules for overloading operators
1. Only existing operators can be overloaded. new operators cannot be
created

2. The overloaded operator must have at least one operand that is type of user
defined.

3. We can’t change the basic meaning of an operator. That is to say we cannot


redefine the plus(+) operator to subtract one value from the other.

4.The Overloaded operators follows the rules of the original operators. They
cannot be overridden.

5 There are some operators that can’t be overloaded.

6 we can’t use friend functions to overload certain operators. however the


member function can be used to overload them .

7.The unary operators, overloaded by the member function, take no explicit


arguments and also no explicit values return, but those overloaded
by means of a friend function, take one reference argument.

8. Binary operator overloaded through a member function take two explicit


arguments

9. When using the binary operators overloaded using a member function, the
left hand side operand must be an object of the relevant class.

10. Binary arithmetic operators such as *,+,- and / must be explicitly return
the value, they must not attempt to change their own arguments.

You might also like