Object Oriented
Programming
(CS1143)
Week 9
Department of Computer Science
Capital University of Science and Technology (CUST)
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
2
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
3
Function Overloading
● Overloading functions enables you to define the functions with the same
name as long as their signatures are different.
4
5
Description
● If you call max with int parameters, the max function that expects int
parameters will be invoked
● If you call max with double parameters, the max function that expects
double parameters will be invoked.
● The C++ compiler determines which function is used based on the
function signature.
6
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
7
Function Overriding
● Function overriding is a concept in object-oriented programming
which allows a function within a derived class to override a
function in its base class usually with a different implementation.
● A common use of function overriding is to provide a default
implementation in the base class, and then overriding with a
specific implementation in the derived class
● Redefining a Function
8
9
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
10
W9-P3.cpp
Object as Function
Parameter
11
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
12
W9-P4.cpp
13
Description
● At line 20, ptr is pointing to an object of the Base class, and at line 21, we
call the function defined in the Base class.
● At line 24, we make the same pointer point to an object of the Derived
class, and at line 25, we tried to call the function defined in the Derived
class
● The result shows that the function defined for the Base class is called both
times.
14
Outline
● Function Overloading
● Function Overriding
● Objects as argument to functions
● Pointer to Objects
● Introduction to Polymorphism
15
Why we need pointers to objects?
● We did not need to use pointers in the program on Slide 13.
● We can use base class’s object (say b1) and write b1.print( ) instead of ptr
−> print()
● We can use derived class’s object (say d1) and write d1.print( ) instead of
ptr −> print().
● However, the program shows the idea where we can use only one pointer
that can point to different objects.
● Assume we need to have an array of objects.
● We know that all elements of an array must be of the same type; this
means we cannot use an array of objects if the objects are of different
types.
● However, we can use an array of pointers, in which each pointer can
point to an object of the base class.
16
Example
● Assume that we have a base class “Person” and a class “Student” derived
from it.
● We can create an array of pointers where each pointer can point to an
object of the base class (Person).
● We can store objects of both Person and Student class in this array.
17
Polymorphism
● A function can be implemented in several classes along the inheritance
chain (Function Overriding).
● There must be a way for the system to decide which function is
invoked (i.e. from which class) at runtime based on the actual type of
the object stored in the pointer.
● This is commonly known as polymorphism (from a Greek word meaning
“many forms”).
18
Enabling Polymorphism
● In the program on Slide 13 (shown here
again), in both cases the function of the
base class was called.
● We can solve this problem if we
declare the function in the base class
as “virtual”.
● This is done using the keyword “virtual”
● Now the appropriate function will be
called based on the type of object.
19
W9-P5.cpp
Using virtual keyword
20
Another Example
21
W9-P6.cpp
22
Constructors and
Destructors
23
Constructors and Destructors
●Constructors cannot be virtual because although
constructors are member functions, the names of the
constructors are different for the base and derived
classes (different signatures).
●Although the names of the destructors differ in the base
and derived classes, the destructors are not normally
called by their name.
●When there is a virtual member function anywhere in the
design, we should also make the destructors virtual to
avoid memory leaks.
●To understand the situation, we discuss two cases:
○ (1) when we are not using polymorphism,
○ (2) when we are using polymorphism.
24
Case 1: No Polymorphism
●Assume that we create a Person class and a Student
class.
●The Person class has a name data member of type
string in which the characters are created in the heap.
○ For data members of type string, even though the object is
in the stack, the characters representing the string are
allocated in the heap
●The Student class inherits name from the Person class,
but it also adds another data member, gpa.
○ Since the derived class inherits the string data member, the
derived class also has a data member allocated in heap
memory
25
26
Case 1: No Polymorphism Contd..
● We cannot have a memory leak in this situation.
● When the program terminates, the destructors for Person class and
Student class are called, which automatically call the destructors of the
string class, which delete the allocated memory in the heap.
27
W9-P7.cpp
28
No Polymorphism. The destructors for Person class and Student class have been called
Case 2: Polymorphism
● With Polymorphism, the situation is different.
● The Person object and the Student object are created in the heap.
The string objects are also created in the heap.
29
Case 2: Polymorphism – No Memory Leak
●We apply the delete operator to the polymorphic variable,
ptr, in stack memory to delete the objects in heap memory.
●In the above scenario, the pointer ptr is a pointer to
Person type and the delete operator can delete the
Person object.
●When the Person object is deleted, its destructor is called,
which in turn calls the destructor of the string class. The
characters created in the heap are de-allocated.
●There is no memory leak.
30
Case 2: Polymorphism – Memory Leak
● In the above scenario, the pointer ptr is still a pointer to Person type,
which means it can delete an object of a Person class (which does not
exist and nothing happens), but it cannot delete the object of the Student
class.
● When the object of the Student class is not deleted, its destructor is
not called, which means that the destructor of the string class is also not
called, which means the characters in the heap are not de-allocated.
● We have memory leak.
31
W9-P8.cpp
The destructor for Student class has not been called 32
Solution
● The solution is to make the destructor of the base class virtual, which
automatically makes the destructor of the derived class virtual.
● In this case, the system allows two different member functions with
different names to be virtual
33
W9-P9.cpp
34
The destructor for Student class has been called now
Example Program
The following program makes a class Shape and then inherits 3 shapes from it, Square,
Circle and Rectangle.
Each class overrides the function getArea() and has its own implementation of this function.
In the main function, we use polymorphism to call getArea().
35
W9-P10.cpp
36
37
38
This is all for Week 9
39