session12
session12
CODEX
HUB
CODE ACADEMY
Session 12
Prepared by Elsayed Mustafa
Polymorphism in C++
15 February, 2024
TODAY’S TOPICS
1. Introduction to Polymorphism
2. Compile-Time Polymorphism
a. Function Overloading
b. Operator Overloading
3. Run-Time Polymorphism
a. Virtual Functions
b. Pure Virtual Functions and
Abstract Classes
4. Common Mistakes in Polymorphism
WHAT IS POLYMORPHISM?
Polymorphism is an object-oriented programming concept
that refers to the ability of a variable, function or object to
take on multiple forms.
With polymorphism, class objects belonging to the same
hierarchical tree (inherited from a common parent class) may
have functions with the same name, but with different
behaviors
WHAT IS POLYMORPHISM?
Example Overview:
A Shape base class with derived classes Circle and
Rectangle. Both derived classes implement a draw()
function differently.
Key Benefits:
Code Reusability
Flexibility in code maintenance
The "is a" Relationship
1. The relationship between a Base Class
and a derived class is called an "is a"
relationship.
2. Example relationships:
3. A postgraduate student "is a" Student.
4. An Employee "is a" Person.
5. A Salaried Employee "is a" Employee.
6. A car "is a" vehicle.
7. Specialized objects have all
characteristics of the general object
plus additional unique traits.
8. Inheritance in OOP creates the "is a"
relationship.
TYPES OF POLYMORPHISM
FUNCTION OVERLOADING
Definition: Function overloading
allows multiple functions with the
same name but different parameter
lists to coexist in the same scope.
Explanation: The sum function is
overloaded to handle different
types and numbers of arguments,
demonstrating flexibility in handling
different data types.
MORE EXAMPLES OF FUNCTION OVERLOADING