0% found this document useful (0 votes)
2 views

session12

The document provides an overview of polymorphism in C++, detailing its types including compile-time and run-time polymorphism, and the use of virtual functions and abstract classes. It emphasizes the importance of polymorphism for code flexibility and reusability, while also addressing common mistakes in its implementation. Additionally, it introduces exception handling as a mechanism for managing runtime errors in C++.

Uploaded by

amoselgpt241
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

session12

The document provides an overview of polymorphism in C++, detailing its types including compile-time and run-time polymorphism, and the use of virtual functions and abstract classes. It emphasizes the importance of polymorphism for code flexibility and reusability, while also addressing common mistakes in its implementation. Additionally, it introduces exception handling as a mechanism for managing runtime errors in C++.

Uploaded by

amoselgpt241
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CODEX

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

Explanation: The max function is overloaded to work with


both integers and double values.
MORE EXAMPLES OF FUNCTION OVERLOADING
Explanation:
The print
function is
overloaded to
handle
integers,
floating-point
numbers, and
strings.
RUN-TIME POLYMORPHISM
Overview: Achieved through inheritance and virtual functions,
where the decision about which function to invoke is made at run-
time.
Importance: Enables the design of flexible and reusable code,
allowing the same function name to be used for different
purposes depending on the context.
VIRTUAL FUNCTIONS
Definition: Virtual functions
allow derived classes to
override the implementation
provided by a base class.
Explanation: The speak
function is overridden in the
Dog and Cat classes. The base
class pointer can call the
appropriate speak function
depending on the type of the
derived class object.
VIRTUAL FUNCTIONS IN ACTION
Explanation: The
makeAnimalSpeak
function accepts an
Animal reference, but
the correct speak
function is called based
on the actual object
passed.
POINTER IN POLYMORPHISM
Allows Dynamic Method Dispatch → Calls the overridden method
in the derived class, not the base class version.
Enables Code Flexibility → A single pointer (Employee* emp;) can
hold different derived objects (Sales, Engineer).
ABSTRACT CLASSES- INTERFACES
An interface (Abstract Class) describes the behavior or capabilities of
a C++ class without committing to a particular implementation of that
class.
The purpose of an abstract class is to provide the Desired base class
Form which will be inherited by other classes in the class hierarchy.
Abstract classes cannot be used to instantiate objects and serves
only as an interface.
A class is made abstract by declaring at least one of its functions as
pure virtual function
A class is made abstract by declaring at least one of its
functions as pure virtual function
PURE VIRTUAL FUNCTIONS AND ABSTRACT CLASSES
Pure Virtual Function: A
function with no definition in
the base class, requiring
derived classes to override it.
Explanation: The Shape class
is abstract and cannot be
instantiated. Circle and
Rectangle must implement the
draw function.
REAL-WORLD EXAMPLE OF RUN-TIME
POLYMORPHISM
Scenario: Designing a GUI library where different types of buttons
(Button, CheckBox, RadioButton) inherit from a common Button
class.
Explanation: Each button type overrides the click function to
provide specific functionality, while the base class ensures that all
buttons share a common interface.
COMMON MISTAKES IN POLYMORPHISM
Not Using virtual Keyword: Omitting the virtual keyword in the base
class can lead to unexpected behavior where the base class function is
called instead of the derived class function.
Confusion Between Overloading and Overriding: Overloading happens
within the same scope, while overriding occurs in the derived class.
Not Defining Pure Virtual Functions in Derived Class: Failing to
implement a pure virtual function in a derived class will make the
derived class abstract, preventing object instantiation.
SUMMARY
Key Points:
Polymorphism enables flexibility and reuse in C++.
Compile-time polymorphism is achieved through function
overloading.
Run-time polymorphism is achieved through inheritance and virtual
functions.
Abstract classes define interfaces for derived classes using pure
virtual functions.
EXCEPTION HANDLING - CONCEPT
What is Exception Handling?
Mechanism to handle runtime errors and exceptional situations
without terminating the program abruptly.
Core Components:
try block: Code that might throw an exception.
catch block: Code to handle the exception.
throw keyword: Used to signal that an exception has occurred.
throw expression: Specifies the type of exception being thrown.
EXCEPTION HANDLING - BASIC EXAMPLE
Explanation:
Demonstrate how
the divide function
throws an exception
when the
denominator is zero,
and the catch block
handles it.
EXCEPTION HANDLING - MULTIPLE EXCEPTIONS
Explanation:
Show how
different
exceptions are
caught and
handled based
on their type.
CONCLUSION
Understanding and effectively using polymorphism allows
for more robust, maintainable, and scalable C++ code
10
Let’s Practice ..
Thank you

You might also like