Overloading

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

lOMoARcPSD|35728696

Operator overloading - It's very useful.

Computer Science (University of Kerala)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Deepu Beniwal ([email protected])
lOMoARcPSD|35728696

Operator Overloading and Type Conversions

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:

1. class member access operators(.,.).


2. scope resolution operator(::).
3. size operator (sizeof) .
4. conditional operator(?:).

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:

return type classname :: operator (op-arglist)


{
Function body. // task define
}

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.

Rules for overloading operators

There are certain restrictions and limitations in overloading operators.

1. OnIy existing operators can be overloaded. New operators cannot be created.

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.

5. There are some operators that cannot be overloaded.

Downloaded by Deepu Beniwal ([email protected])


lOMoARcPSD|35728696

6. We cannot use friend functions to overload certain operators. However, member


functions can be used to overload them.

7. Unary operators, overloaded by means of a member function, take no explicit


arguments and return no explicit values, but, those overloaded by means of a friend
function, take one reference argument (the object of the relevant class).

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.

Operator functions must either be member functions (non-static) or friend functions. A


basic difference between them is that a friend function will have only one argument for
unary operators and two for binary operators, while a member function has no
arguments for unary operators and only one for binary operators.

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:

1. It must be a class member.


2. It must not specify a return type.
3. It must not have any arguments.

Downloaded by Deepu Beniwal ([email protected])

You might also like