You are on page 1of 18

OPERATOR OVERLOADING

PDPU
INTRODUCTION…

 We use different operators like +, -, * on basic data types


like int, float or double.

 Can we use them for user defined data types like array,
structure or class?

2
INTRODUCTION…

 EXAMPLE
char name1[30] = “Pandit Dindayal ” ;
char name2[30] = “Petroleum University”;
char fullname[60];
strcpy (fullname,name1);
strcat (fullname, name2);

Instead of the above program, can we write ,


fullname = name1 + name2;
3
DEFINING OPERATOR
OVERLOADING…

 In C, No.
 In C++, Yes.
 It is known as operator overloading, or compile time
polymorphism.
 Operator overloading teaches a normal operator to act on a
user-defined operand.
 It means giving additional meaning to operators like +, *, - ,
++ , -- .
 User-defined data types behave in much the same way as
the standard data types. 4
DEFINING OPERATOR
OVERLOADING…

 We must have to specify what an additional task to an


operator means in relation to the class to which the operator
is applied.
 It is done with the help of a special function known as
operator function that describes the task.

5
GENERAL FORM OF OPERATOR
FUNCTION…

return-type classname :: operator op(arglist)


{
function body; // task defined
}
HERE,
 return-type is the type of value returned by the specified
operator.
 op is the operator being overloaded. e.g. + *

 op is preceded by keyword operator. 6

 operator op is the function name.


EXAMPLE…

// definition…
complex operator + ( complex c)
{
complex t;
t.real = real + c.real;
t.imag = imag + c.imag;
return t;
}
7

Pl. go through the program opovrld1.cpp


EXAMPLE…

// Call…
c3 = c1 + c2;
 When the operator +() function is called,
 The object c2 is passed to it and collected in the
object c.
 The object c1 gets passed to it automatically.

 compiler treats the above statement as


 c3 = c1.operator+ (c2);

8
EXAMPLE…

t.real = real + c.real;


HERE,
real refers to the one that belongs to the object using which
the operator function has been called.
In c3 = c1 + c2; statement,
real represents c1’s real.

9
PROCESS OF OVERLOADING…

1. Create a class that defines the data type that is to be used


in the overloading operation.
2. Declare the operator function in the public part of the
class. It may be either a member function or a friend
function.
3. Define the operator function to implement the required
operations.

10
FEW POINTS…

1. Operator function must be either member functions or


friend functions.
1. A friend function will have only one argument for unary
operators and two for binary operators.
2. A member function has no argument for unary operators and
only one argument for binary operators.
2. We cannot change its syntax, the grammatical rules that
govern its use such as no. of operands, precedence and
associativity.

11
FEW POINTS…

3. We cannot overload the following C++ operators:

1. class member access operators: . .*


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

12
FEW POINTS…

4. We cannot overload the following C++ operators when we


use friend function:
1. Assignment Operator : =

2. Function call operator : ()

3. Subscripting Operator : []

4. Class Member Access Operator : 


13
OVERLOADING UNARY OPERATOR…

 Unary operators act on only one


operand.
 Example: unary minus, ++ , -- .
 pl. go through opovrld1.cpp for unary
minus.
 pl. go through opovrld2.cpp for ++, --.
14
EXERCISE…

 Create a class FLOAT that contains


one float data member. Overload all
the four arithmetic operators so that
they operate on the objects of FLOAT.
 Define a class STRING. Use
overloaded == operator to compare
two strings.
15
EXERCISE…

 Define a class STRING. Use overloaded +


operator to join two strings.
 Create a class MAT of size 3 x 3. Perform
addition , subtraction, multiplication of two
MAT using operator overloading.

16
SUMMARY…

 Operator overloading is one of the important features of


C++ language.
 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:
 . .* :: sizeof ?:
 It is done with the help of operator function.
17
SUMMARY…

 It must either be member function or friend function.


 Friend function will have two arguments for binary
operator and one argument for unary operator.
 Member function will have one argument for binary
operator and nil argument for unary operator.
 There must be at least one operand of user-defined type.

18

You might also like