Operator overloading allows operators like + and - to be used with user-defined types by defining special operator functions. These functions specify the task to be performed and have a return type and take the className as the first argument. Operator functions can be member functions or friend functions. Certain operators like ., :: cannot be overloaded. Overloaded operators must follow the syntax of the original operators and cannot change the basic meaning of an operator.
2. Operator Overloading
• Operator Overloading :
– C++ permits us to add two variables of user-defined types with the same
syntax that is applied to the basic type.
– This means that C++ has the ability to provide the operators with a special
meaning for a data type.
– The mechanism of giving such special meaning to an operator is known as
operator overloading.
• Defining Operator Overloading :
– To define an additional task to an operator, we must specify it to the class to
which the operator is applied. This is done with the help of a special function,
called operator function, which describes the task.
– Syntax : return-type className :: operator op(arg_list)
{
function body // task defined
}
3. Operator Overloading (continue…)
• The op is the operator being overloaded. The op is preceded by the
keyword operator. Operator op is the function name.
• Operator functions must be either 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.
• This is because the object used to invoke the member function is
passed implicitly and therefore is available for the member
function. This is not the case with friend functions.
• Arguments may be passed either by value or by reference.
4. Operator Overloading (continue…)
• Rules For Overloading Operators :
– Only existing operators can be overloaded. New operators can not be created.
– The overload operator must have at least one operand that is of user-defined
type.
– 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.
– Overloaded operators follow the syntax rules of the original operators. They
cannot be overridden.
– There are some operators that cannot be overloaded.
. (Dot operator) Class member access operator
.* Class Member access operator
:: Scope resolution operator
? : Conditional Operator
sizeof -> sizeof operator
5. Operator Overloading (continue…)
– The following operators can not be overloaded by friend function.
• [ ] Subscripting operator
• ( ) Function call operator
• = Assignment operator
• -> Member access operator