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

Lecture-08 Polymorphism

Uploaded by

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

Lecture-08 Polymorphism

Uploaded by

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

Object Oriented

Programming
Polymorphism
Polymorphism
• Basic concept of Object Oriented Programming
• The word “polymorphism” means having many forms. In simple
words, something that has ability to change.
• A real-life example of polymorphism is a person who at the same
time can have different characteristics. A man at the same time is a
father, a husband, and an employee. So the same person exhibits
different behavior in different situations.
Types of polymorphism
• Compile-time Polymorphism
• Function Overloading
• Operator Overloading
• Runtime Polymorphism
• Virtual Functions
• Function Overriding
Compile Time Polymorphism
Function Overloading
• When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this
is known as Function Overloading.
• Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments.
• it is a feature of object-oriented programming providing many
functions that have the same name but distinct parameters when
numerous tasks are listed under one function name.
Operator Overloading
• C++ has the ability to provide the operators with a special meaning
for a data type.
• For example, we can make use of the addition operator (+) for string
class to concatenate two strings.
• We know that the task of this operator is to add two operands. So a
single operator ‘+’, when placed between integer operands, adds
them and when placed between string operands, concatenates them.
Operator Overloading
• Term operator overloading refers to giving the normal C++ operators,
such as +,-,*, ==, +=, additional meaning when they applied to user
defined data types.
• Example:
a = b+c
works only with basic types such as int and float, and attempting to
apply it when a,b,c are objects of user-defined class will cause error
from compiler.
• However by using Operator overloading you can make statement legal
even when a, b, c are user defined types.
Overload Unary Operator
• As you know unary operator act on only one operand.
• Example of unary operator are increment(++) and decrement(--) .
• See next slide to understand unary operator overloading
Overload Binary Operator
• In binary operator overloading function, there should be one
argument to be passed. It is overloading of an operator operating on
two operands.
• For example:
c= a+b;
• here a, b, c are object of user defined class.
• Binary operators are +, -, =, +=, -=, ==,<=,>= etc.
• Example of binary operator overloading is given into next slide.
Overload Binary Operator
Binary Operator Overloading
• Practice some examples to overload Assignment and comparative
operators(=, ==).
Run Time Polymorphism
Function Overriding
• As we know, inheritance is a feature of OOP that allows us to create
derived classes from a base class. The derived classes inherit features
of the base class.
• Suppose, the same function is defined in both the derived class and
the based class.
• Now if we call this function using the object of the derived class, the
function of the derived class is executed.
• This is known as function overriding in C++. The function in derived
class overrides the function in base class.
Function Overriding
• Function overriding in C++ is termed as the redefinition of base class
function in its derived class with the same signature i.e. return type
and parameters.
Access Overridden Function to the Base class
• To access the overridden function of base class , scope resolution
operator is used (::).
Call Overridden Function Using Pointer
Virtual function
• Virtual means existing in appearance but not in reality.
Pure Virtual Function
• Sometimes implementation of all functions cannot be provided in a
base class because we don’t know the implementation. Such a class is
called an abstract class.
• For example, let Shape be a base class. We cannot provide the
implementation of function draw() in Shape, but we know every
derived class must have an implementation of draw(). Similarly, an
Animal class doesn’t have the implementation of move() (assuming
that all animals move), but all animals must know how to move.
• We cannot create objects of abstract classes.
Pure Virtual Function
• A pure virtual function (or abstract function) is a virtual function for
which we can have an implementation, But we must override that
function in the derived class, otherwise, the derived class will also
become an abstract class.
• A pure virtual function is implemented by classes that are derived
from an Abstract class.
• A class is abstract if it has
atleast one pure virtual function.
Output
Normal Member Functions Accessed with
Pointers
• See next slide:
Output
Normal Member Functions Accessed with
Pointers
Virtual Member Functions Accessed with
Pointers
• See next slide

You might also like