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

Polymorphism in C++

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)
25 views16 pages

Polymorphism in C++

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

Polymorphism in C++

Dr. Muhammad Sadiq Amin


Introduction
• Polymorphism means "many forms."
• It refers to the ability of a function, operator, or object to behave
differently based on the context in which it is used.
• It is one of the fundamental principles of Object-Oriented
Programming (OOP).
Real-Life Example
• A single person can have multiple roles depending on the situation:
• As a father
• As an employee
• As a friend
This is analogous to how polymorphism allows the same function or
operator to perform different tasks.
Types of Polymorphism in C++
• Polymorphism in C++ can be broadly classified into:
1.Compile-Time Polymorphism (also called static binding or early
binding)
2.Runtime Polymorphism (also called dynamic binding or late binding)
1. Compile-Time Polymorphism
• This type of polymorphism is resolved during program compilation. It
is achieved using:
• Function Overloading
• Operator Overloading
Function Overloading
• When multiple functions share the same name but differ in their
parameters (type, number, or both), they are considered overloaded.
The function to be called is determined at compile time.
Operator Overloading
• C++ allows overloading operators to provide them custom behavior
for user-defined data types.
• For example, the + operator can be overloaded to add two complex
numbers.
2. Runtime Polymorphism
• This type of polymorphism is achieved during runtime and is
implemented using:
• Function Overriding
• Virtual Functions
Function Overriding
• When a derived class provides a specific implementation for a
function that is already defined in its base class, it overrides the base
class's implementation.

• To achieve runtime polymorphism, the function in the base class must


be declared virtual.
Watch the difference
• See this example in the notes below
Virtual Functions
• A virtual function is a member function declared in the base class and
overridden in the derived class.
• It ensures that the correct function is called for an object, regardless
of the type of reference or pointer used.
Rules for Virtual Functions :
1. Cannot be static.
2. Must be declared in the base class and optionally overridden in the
derived class.
3. Access through a base class pointer/reference.
4. Use the virtual keyword only in the base class.
Pure Virtual Functions
• A pure virtual function is a virtual function with no implementation in
the base class. It forces derived classes to implement the function.
• A class containing one or more pure virtual functions is called an
abstract class.
Key Takeaways
• Polymorphism enhances code flexibility and reusability.
• Compile-Time Polymorphism is efficient but less flexible.
• Runtime Polymorphism adds flexibility but requires careful use of
virtual functions.
• Abstract classes and pure virtual functions enforce a contract for
derived classes.
Task
• Design a polymorphic e-commerce system in C++ with the following requirements:

• 1. Create a base class `Product` with common attributes (`name`, `price`) and a virtual method
`getDescription()`.
• 2. Derive two subclasses:
• - Book: Add an `author` attribute and override `getDescription()` to include book details.
• - Electronics: Add a `brand` attribute and override `getDescription()` to include electronics details.
• 3. Use a static array of `Product` pointers to store objects of `Book`, `Electronics`, and `Product`.
• 4. Write a function to iterate through the array and display the details of each product using
polymorphism.
• 5. Ensure proper memory management by deleting dynamically allocated objects.

• Demonstrate the program with at least one object of each class.


Solution

You might also like