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

Object Oriented Programming: Polymorphism and Abstract Classes

This document discusses object oriented programming concepts of polymorphism and abstract classes. It defines polymorphism as an object having many forms, allowing a message to be displayed in multiple forms. It describes different types of polymorphism like compile-time and runtime polymorphism. Runtime polymorphism is achieved through function overriding using virtual functions. Abstract classes are classes with at least one pure virtual function, meaning the function has no implementation and must be implemented in derived classes.

Uploaded by

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

Object Oriented Programming: Polymorphism and Abstract Classes

This document discusses object oriented programming concepts of polymorphism and abstract classes. It defines polymorphism as an object having many forms, allowing a message to be displayed in multiple forms. It describes different types of polymorphism like compile-time and runtime polymorphism. Runtime polymorphism is achieved through function overriding using virtual functions. Abstract classes are classes with at least one pure virtual function, meaning the function has no implementation and must be implemented in derived classes.

Uploaded by

Mohsin ikram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Object Oriented Programming

Polymorphism and
Abstract Classes
Introduction
• The word polymorphism means having many
forms. In simple words, we can define
polymorphism as the ability of a message to be
displayed in more than one form.
• Real life example of polymorphism, a person at a
same time can have different characteristic. Like a
man at a same time is a father, a husband, a
employee. So a same person posses different
behavior in different situations. This is called
polymorphism.
Types of Polymorphism
• Compile time Polymorphism
– This type of polymorphism is achieved by function
overloading or operator overloading.
• Runtime Polymorphism
– This type of polymorphism is achieved by Function
Overriding.
Run Time Polymorphism
• Function overriding on the other hand occurs
when a derived class has a definition for one
of the member functions of the base class.
That base function is said to be overridden.
Early Binding
Early Binding Cond..
• The reason for the incorrect output in both
previous programs is that the call of the function
getName() and speak() is being set once by the
compiler as the version defined in the base class.
This is called static resolution of the function call,
or static linkage - the function call is fixed before
the program is executed. This is also sometimes
called early binding because the function
resolution is done during the compilation of the
program.
Late Binding
• But now, let's make a slight modification in
our program
• keyword virtual so that it looks like this −
Virtual Functions and Polymorphism

A virtual function is a member function of the base class, that is overridden in


derived class. The classes that have virtual functions are called polymorphic
classes.
The compiler binds virtual function at runtime, hence called runtime
polymorphism. Use of virtual function allows the program to decide at runtime
which function is to be called based on the type of the object pointed by the
pointer.
A derived function is considered a match if it has the same signature (name,
parameter types, and whether it is const) and return type as the base version
of the function. Such functions are called overrides.
Revisiting previous example
Late Binding Contd.
• This time, the compiler looks at the contents of
the pointer instead of it's type. Hence, since
addresses of objects of Cat and dog classes are
stored in *pAnimal the respective speak()
function is called.
• As you can see, each of the child classes has a
separate implementation for the function
speak(). This is how polymorphism is generally
used. You have different classes with a function of
the same name, and even the same parameters,
but with different implementations.
Return type of virtual function
• Must Match

In this case, Derived::getValue() is not considered a matching override for


Base::getValue() (it is considered a completely separate function).
Rules for virtual functions
• Virtual functions cannot be static and also cannot be a
friend function of another class.
• Virtual functions should be accessed using pointer or
reference of base class type to achieve run time
polymorphism.
• The prototype of virtual functions should be same in
base as well as derived class.
• They are always defined in base class and overridden in
derived class. It is not mandatory for derived class to
override (or re-define the virtual function), in that case
base class version of function is used.
Virtual Destructors
You should always make your destructors virtual
if you’re dealing with inheritance.
Run both codes and see difference
Pure Virtual Functions
• It is possible that you 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 that there is no meaningful
definition you could give for the function in
the base class.
• We can change the virtual function speak() in
the base class to the following −
• The = 0 tells the compiler that the function
has no body and above virtual function will be
called pure virtual function.
Abstract Classes and Methods
• Generally we understand a pure abstract class
as any class having only abstract methods.
• Method abstraction is a very useful technique
to define behavior patterns over the classes
that are going to inherit from a given class we
are defining.
• An abstract method is a class method which
won’t be implemented but is expected to be
implemented by the descendants of the class.
Contd..
• Sometimes implementation of all function cannot be
provided in a base class because we don’t know the
implementation. Such a class is called abstract class.
• For example, let Shape be a base class. We cannot
provide implementation of function draw() in Shape,
but we know every derived class must have
implementation of draw(). Similarly an Animal class
doesn’t have implementation of move() (assuming that
all animals move), but all animals must know how to
move. We cannot create objects of abstract classes.
Contd..
• A pure virtual function (or abstract function)
in C++ is a virtual function for which we don’t
have implementation, we only declare it. A
pure virtual function is declared by assigning 0
in declaration. See the following example.
• A pure virtual function is implemented by
classes which are derived from a Abstract
class. Following is a simple example to
demonstrate the same
Some Interesting Facts
1. A class is abstract if it has at least one pure
virtual function. In the following example,
Test is an abstract class because it has a pure
virtual function show().
2. We can have pointers and references of
abstract class type but not objects. For example
the following program works fine.
3. If we do not override the pure virtual function
in derived class, then derived class also becomes
abstract class. The following example
demonstrates the same.
4. An abstract class can have constructors.
For example, the following program compiles
and runs fine.

You might also like