0% found this document useful (0 votes)
3 views16 pages

13 Polymorphism

The document outlines key concepts of Object Oriented Programming (OOP), focusing on polymorphism, pure virtual functions, abstract classes, and virtual inheritance. It explains runtime polymorphism through function overriding and the significance of virtual functions in enabling dynamic binding. Additionally, it addresses the diamond shape problem in multiple inheritance and how virtual inheritance resolves ambiguity by ensuring a single instance of a common base class.

Uploaded by

f2024065470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

13 Polymorphism

The document outlines key concepts of Object Oriented Programming (OOP), focusing on polymorphism, pure virtual functions, abstract classes, and virtual inheritance. It explains runtime polymorphism through function overriding and the significance of virtual functions in enabling dynamic binding. Additionally, it addresses the diamond shape problem in multiple inheritance and how virtual inheritance resolves ambiguity by ensuring a single instance of a common base class.

Uploaded by

f2024065470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming

Instructor: Shinawar Naeem


Department of Software Engineering
Semester: Spring 2025
Course Code: CC-1022
Objectives
• Polymorphism
• Pure Virtual Functions
• Abstract class
• Virtual Inheritance
• The Diamond Shape Problem
Polymorphism
• Polymorphism allows you to write code that can work with objects of
different classes, as long as they share a common base class or
interface. This means that you can write generic code that operates on
a base class type, and it can be used with any derived class that
inherits from that base class.
Runtime polymorphism
• In a Runtime polymorphism, functions are called at the time the
program execution. Hence, it is known as late binding or dynamic
binding.
• Function overriding is a part of runtime polymorphism. In function
overriding, more than one method has signature.
• It is achieved by using virtual functions and pointers. It provides slow
execution as it is known at the run time. Thus, It is more flexible as all
the things executed at the run time.
function overriding
• In function overriding, we give the new definition to base class
function in the derived class. At that time, we can say the base
function has been overridden.
• It can be only possible in the ‘derived class’. In function overriding, we
have two definitions of the same function, one in the superclass and
one in the derived class.
• The decision about which function definition requires calling happens
at runtime. That is the reason we call it ‘Runtime polymorphism’.
Virtual Function
• A virtual function is declared by keyword virtual. The return type of virtual function may be
int, float, void.
• A virtual function is a member function in the base class. We can redefine it in a derived class.
It is part of run time polymorphism. The declaration of the virtual function must be in the base
class by using the keyword virtual. A virtual function is not static.
• The virtual function helps to tell the compiler to perform dynamic binding or late binding on
the function.
• If it is necessary to use a single pointer to refer to all the different classes’ objects. This is
because we will have to create a pointer to the base class that refers to all the derived objects.
• But, when the base class pointer contains the derived class address, the object always
executes the base class function. For resolving this problem, we use the virtual function.
• When we declare a virtual function, the compiler determines which function to invoke at
runtime.
Explanation
• In this example, we have a base class called Shape with a virtual draw() function. The Shape class
serves as a common interface for all the derived classes.

• We have two derived classes, Circle and Square, which inherit from the Shape base class. Each
derived class overrides the draw() function to provide its own implementation.

• In the main() function, we create two pointers of type Shape*, shape1 and shape2. We assign a
Circle object to shape1 and a Square object to shape2. Even though the pointers are of type
Shape*, they can hold objects of different derived classes because of polymorphism.

• When we call the draw() function on shape1 and shape2, the appropriate version of the function
is called based on the actual type of the object being referred to at runtime. This is called dynamic
binding or late binding. The draw() function in each derived class is invoked, and the
corresponding message is printed.
Polymorphism Importance
• Polymorphism allows us to write generic code that can
handle different types of shapes without knowing the
specific derived class at compile time.
• It enables code reuse, extensibility, and flexibility in
object-oriented systems.
Pure virtual functions
• Pure virtual functions, also known as abstract functions, are virtual
functions in the base class that have no implementation.
• They are declared with the virtual keyword and assigned a value of 0
using the = 0 syntax.
• Pure virtual functions act as placeholders for functions that must be
implemented in the derived classes.
Abstract Class
• An abstract class is a class that is designed to be a base
class for other classes but cannot be instantiated on its
own. It typically contains at least one pure virtual
function, making it an abstract base class. An abstract
class provides a common interface or contract that
derived classes must implement.
Explanation
 In this example, the Shape class is an
abstract class with a pure virtual function
draw(). This means that any class derived
from Shape must implement the draw()
function. The Circle and Rectangle classes
inherit from Shape and provide their own
implementations of the draw() function.

 Since the Shape class is abstract, objects


cannot be created directly from it.
However, pointers of type Shape* can be
used to hold objects of derived classes,
allowing for polymorphism and a common
interface.
The diamond shape problem
• The diamond shape problem occurs in multiple
inheritance when a class derives from two classes, and
both of those classes have a common base class. This
creates a diamond-shaped inheritance hierarchy,
leading to potential issues related to ambiguity and
duplicate member variables.
Explanation
 In this example, both the Mammal
and Bird classes inherit from the
Animal class, and the Platypus class
inherits from both Mammal and Bird.
When we try to call the eat() function
on a Platypus object, an ambiguity
arises because the Platypus class has
two eat() functions inherited from
Mammal and Bird, which in turn
inherited from Animal.

 To resolve the diamond shape


problem, we can use virtual
inheritance to ensure that there is
only one instance of the common
base class in the inheritance hierarchy,
as shown in the previous example of
virtual inheritance.
Virtual inheritance
• Virtual inheritance is a feature in C++ that allows a
class to inherit from a base class in a way that avoids
duplication of inherited members.
• It resolves issues that may arise when a class inherits
from multiple classes that have a common base class.
• It ensures that only one instance of the base class is
included in the inheritance hierarchy.
Explanation
 In this example, the Animal class is
the base class, and both the
Mammal and Bird classes virtually
inherit from it using the virtual
keyword. The Platypus class then
inherits from both the Mammal and
Bird classes.
 With virtual inheritance,
there is only one instance of
the Animal base class within
the Platypus object,
eliminating any duplication
or ambiguity that may arise
from multiple inheritance.

You might also like