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

C++ OOP - Polymorphism

Polymorphism in C++ allows functions to execute differently based on the object type invoking them, typically within a class hierarchy. Virtual functions are essential for achieving polymorphism, enabling derived classes to redefine base class functions. Pure virtual functions serve as placeholders in base classes for functions that must be implemented in derived classes, indicating that the base class cannot provide a meaningful implementation.

Uploaded by

vinnoval123
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 views11 pages

C++ OOP - Polymorphism

Polymorphism in C++ allows functions to execute differently based on the object type invoking them, typically within a class hierarchy. Virtual functions are essential for achieving polymorphism, enabling derived classes to redefine base class functions. Pure virtual functions serve as placeholders in base classes for functions that must be implemented in derived classes, indicating that the base class cannot provide a meaningful implementation.

Uploaded by

vinnoval123
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/ 11

C++ PROGRAMMING

OOP - POLYMORPHISM
WHAT IS POLYMORPHISM ?

• The word polymorphism means having many forms.


Typically, polymorphism occurs when there is a hierarchy of
classes and they are related by inheritance.
• C++ polymorphism means that a call to a member function
will cause a different function to be executed depending on
the type of object that invokes the function.
WHAT IS POLYMORPHISM ?
Shape class has its
own implementation
of area(). So the
derived class of
Rectangle and Triangle
cannot just call area()
because we all know
that Rectangle and
Triangle is different in
getting the area. So
we need to have
area() in each derived
classes. This is the use
of polymorphism.
WHAT IS VIRTUAL FUNCTION ?
• In order to realized polymorphism, we need to
declare virtual function.
• A virtual function is a function in a base class that is
declared using the keyword virtual. Defining in a
base class a virtual function, with another version in
a derived class, signals to the compiler that we can
refer to the to the version of derived class’ function.
Lets have an example in next slide. ☺
HOW TO USE VIRTUAL FUNCTION ?
• This is how to declare virtual function in the class.
This is usually put in the base class.

virtual data-type function-name()

• Lets have an example in the next slide ☺


EXAMPLE#1 (WITHOUT VIRTUAL)

OUTP
UT:
EXAMPLE#2 (WITH VIRTUAL)

OUTP
UT:
WHAT IS PURE VIRTUAL FUNCTION ?
• It's possible that you'd want to include a virtual
function in a base class so that it may be redefined
in a derived class to suit the objects of that class,
but there is no meaningful definition you could give
for the function in the base class. Like the Shape
class, there is such way to determine its area
because it is not specific.

• WARNING: If you have at least one pure virtual


HOW TO USE PURE VIRTUAL FUNCTION ?
• This is how to declare pure virtual function in the
class. This is usually put in the base class.

virtual data-type function-name() = 0

• Lets have an example in the next slide ☺


• We can change the virtual function area()
in the base class into pure virtual since its
implementation has no use at all.
EXAMPLE#3 (WITH PURE VIRTUAL)

OUTP
UT:

You might also like