Page 147 - DCAP404 _Object Oriented Programming
P. 147
Object-oriented Programming
Notes 3. Overloading an operator simply means attaching additional meaning and semantics to
an………………………... .
4. Overloading assumes you specify a behavior for an operator that acts on a user defined
type and it can’t be used just with general…………………. .
5. The operator keyword declares a function specifying what operator-symbol means when
applied to instances of a…………………... .
7.2 Rules for Overloading Operators
To overload any operator, we need to understand the rules applicable Let us revise some of
them which have already been explored.
Following are the operators that cannot be overloaded.
Table 7.1: Operators that cannot be Overloaded
Operator Purpose
. Class member access operator
.* Class member access operator
:: Scope Resolution Operator
?: Conditional Operator
Sizeof Size in bytes operator
# Preprocessor Directive
= Assignment operator
0 Function call operator
0 Subscripting operator
-> Class member access operator
1. Operators already predefined in the C++ compiler can be only overloaded. Operator
cannot change operator templates that is for example the increment operator ++ is used
only as unary operator. It cannot be used as binary operator.
2. Overloading an operator does not change its basic meaning. For example assume the +
operator can be overloaded to subtract two objects. But the code becomes unreachable.
class integer
{intx, y;
public:
int operator + ();
}
int integer: : operator + ( )
{
return (x-y);
}
3. Unary operators, overloaded by means of a member function, take no explicit argument
and return no explicit values. But, those overloaded by means of a friend function take one
reference argument (the object of the relevant class).
140 LOVELY PROFESSIONAL UNIVERSITY