Lecture 9-Week9_Function Overloading and Overriding
Lecture 9-Week9_Function Overloading and Overriding
Programming
(CS1143)
Week 9
● 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
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?
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
18
Enabling Polymorphism
19
W9-P5.cpp
20
Another Example
21
W9-P6.cpp
22
Constructors and
Destructors
23
Constructors and Destructors
25
26
Case 1: No Polymorphism Contd..
27
W9-P7.cpp
28
No Polymorphism. The destructors for Person class and Student class have been called
Case 2: Polymorphism
29
Case 2: Polymorphism – No 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 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