Overloading
Overloading
Overloading
Operator overloading is one of the important features of C++ Ianguage. It is called compile time
polymorphism. Using overloading feature we can add two user defined data types such as
objects, with the same syntax, just as basic data types. We can overload almost all the C
operators except the following:
Operator overloading is done with the help of a special function, called operator function, which
describes the special task to an operator. The general form of an operator function is:
where return type is the type of value returned by the specified operation and op is the
operator being overloaded. The op is preceded by the keyword operator . operator op is the
function name.
2. The overloaded operator must have at least one operand that is of user-defined type.
3. We cannot 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. Overloaded operators follow the syntax rules of the original operators. They cannot be
overridden.
8. Binary operators overloaded through a member function take one explicit argument and
those which are overloaded through a friend function take two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand
operand must be an object of the relevant class.
10. Binary arithmetic operators such as + , -, *, and / must explicitly return a value. They
must not attempt to change their own arguments.
The overloading operator must have at least one operand that is of user-defined type.
The compiler does not support automatic type conversions for the user defined data
types. We can use casting operator functions to achieve this. The casting operator
function should satisfy the following conditions: