Unit 3.
1
• 3.1 Constructors and Destructors :
• Introduction, Constructors, Parameterized constructors , multiple
constructors in a class, constructor with default arguments, dynamic
initialization of objects, Copy Constructor, Dynamic constructors,
Destructors
• 3.2 Operator Overloading – Introduction, defining operator overloading,
overloading unary operators, overloading binary operators, rules for
operator overloading.
1
INTRODUCTION
• Operator overloading is one of the many excite.ng features of C++ language.
• For instance, C++ permits us to add two variables of user defined types
with the same syntax that is applied to the basic types.
• This means that C++ has the ability to provide to operators with a
special meaning for a data type.
• The mechanism of giving such special meanings to an operator is known
as operator overloading.
2
• Operator overloading provides a flexible option for the creation of new
definitions for most of the C++ operators.
• We can overload (give additional meaning to) all the C++ operators
except the following:
– Class member access operators (.,.*)
– Scope resolution operator (::)
– Size operator (sizeof)
– Conditional operator (?:)
• The reason why we cannot overload these operators may be attributed
to the fact that these operators take names (example class name) as their
operand instead of values, as is the case with other normal operators
3
DEFINING OPERATOR OVERLOADING
• To define an additional task to an operator, we must specify what it
means in relation to the class to which the operator is applied.
• This is done with the help of a special keyword, called operator, which
describes the task.
• 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.
• operator op is the function name, where operator is a keyword.
4
• Operator functions must be either member functions (nonstatic) 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.
5
• Operator functions are declared in the class using prototypes as follows:
• vector is a data type of class and may represent both magnitude and
direction (as in physics and engineering) or a series of points called
elements (as in mathematics).
6
• The process of overloading involves the following steps.
– Create a class that defines the data type that is to be used in the
overloading operation.
– Declare the operator function operator op() in the public part of the
class. It may be either a member function or a friend function.
– Define the operator function to implement the required operations.
7
8
OVERLOADING UNARY OPERATORS
• Let us consider the unary minus operator.
• A minus operator when used as a unary, takes just one operand.
• We know that this operator changes the sign of an operand when
applied to a basic data item.
• We will see here how to overload this operator so that it can be applied
to an object in much the same way as is applied to an int or float
variable.
• The unary minus when applied to an object should change the sign of
each of its data items.
9
10
11
12
OVERLOADING BINARY OPERATORS
• The same mechanism can be used to overload a binary operator.
• A statement like into an object.
• Such an object is called a temporary object and goes out of space as soon
as the contents are assigned to another object.
• Using temporary objects can make the code shorter, more efficient and
better to read.
13
14
RULES FOR OVERLOADING OPERATORS
• Although it looks simple to redefine the operators, there are certain restrictions and
limitations in overloading them.
• Some of them are listed below:
1. Only 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. Overloaded operators follow the syntax rules of the original operators. They
cannot be overridden.
4, There are some operators that cannot be overloaded. (See Table 7.1.)
5. We cannot use friend functions to overload certain operators, However,
member functions Can used to overload them. (See Table 7.2.)
15
6. 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).
7. Binary operators overloaded through a member function take one
explicit argument and those while overloaded through a friend function take
two explicit arguments.
8. When using binary operators overloaded through a member function,
the left-hand operand must be an object of the relevant class.
9. Binary arithmetic operators such as +, -, *, and / must explicitly return a
value. They must not attempt to change their own arguments
16
17
• LIMITATIONS
1. Some of the operators like ::, -> and Size of() Cannot be over loaded.
2. By operator overloading you cannot change the precedence, associative
and number of arguments of an operator.
18
OVERLOADING BINARY OPERATORS USING FRIEND
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35