0% found this document useful (0 votes)
13 views23 pages

Unit IV Oopm

Uploaded by

Raghuveer Singh
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)
13 views23 pages

Unit IV Oopm

Uploaded by

Raghuveer Singh
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/ 23

Chameli Devi Group of Institutions, Indore

Department of Computer Science


Object Oriented Programming &
Methodology (CS-305)
UNIT IV
Content
 Polymorphism: Introduction,

 Method Overriding,

 Method Overloading,

 Static Polymorphism,

 Run time Polymorphism


Polymorphism
 The term "Polymorphism" is the combination of
"poly" + "morphs" which means many forms.

 In simple words, we can define polymorphism as the ability of


a message to be displayed in more than one form.

 A person at the same time can have different characteristic.

 Like a man at the same time is a father, a husband, an


employee. So the same person posses different behavior in
different situations.
In C++ polymorphism is mainly divided into
two types:

Compile time Polymorphism

Runtime Polymorphism
1) Compile time polymorphism:

 The overloaded functions are invoked by


matching the type and number of arguments.

 This information is available at the compile


time and, therefore, compiler selects the
appropriate function at the compile time.

 It is achieved by function overloading and


operator overloading which is also known as
static binding or early binding.
Example:

We have two functions with same name but different


number of arguments.

Based on how many parameters we pass during


function call determines which function is to be
called.

This is why it is considered as an example of


polymorphism because in different conditions the
output is different.

Since, the call is determined during compile time


that's why it is called compile time polymorphism.
A. Function Overloading:

 When there are multiple functions with


same name but different parameters then
these functions are said to be overloaded.

 Functions can be overloaded by change in


number of arguments or/and change in
type of arguments.
B. Operator Overloading:

 C++ also provide option to overload


operators.

 For example, we can make the operator (‘+’)


for string class to concatenate two strings.

 We know that this is the addition operator


whose task is to add two operands.
 So a single operator ‘+’ when placed between
integer operands, adds them and when
placed between string operands,
concatenates them.

 Additional meaning of an operator.

 Normal function of an operator still exists.


These operators cannot be overloaded
because if we overload them it will make
serious programming issues.

For an example the sizeof operator returns


the size of the object or datatype as an
operand. This is evaluated by the compiler. It
cannot be evaluated during runtime. So we
cannot overload it.
2) Run time polymorphism:

 Run time polymorphism is achieved when


the object's method is invoked at the run
time instead of compile time.

 It is achieved by method overriding which is


also known as dynamic binding or late
binding.
Function Overriding:

When child class declares a method, which is already


present in the parent class then this is called function
overriding, here child class overrides the parent class.

In case of function overriding we have two definitions


of the same function, one is parent class and one in
child class.

The call to the function is determined at runtime to


decide which definition of the function is to be called,
that's the reason it is called runtime polymorphism.
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 don't want
static linkage for this function.
 If there are member function with same name
in base class and derived class, virtual
functions gives programmer capability to call
member function of different class by a same
function call depending upon different
context.

 Here, the selection of the function to be


called at any given point in the program to be
based on the kind of object for which it is
called. This sort of operation is referred to
as dynamic linkage, or late binding.
Differences b/w compile time and run time
polymorphism
Compile time polymorphism Run time polymorphism
The function to be invoked is The function to be invoked is
known at the compile time. known at the run time.

It is also known as overloading, It is also known as overriding,


early binding and static binding. Dynamic binding and late binding.

Overloading is a compile time Overriding is a run time


polymorphism where more than polymorphism where more than
one method is having the same one method is having the same
name but with the different name, number of parameters and
number of parameters or the type the type of the parameters.
of the parameters.
Differences b/w compile time and run time
polymorphism
Compile time polymorphism Run time polymorphism

It is achieved by function It is achieved by virtual functions


overloading and operator and pointers.
overloading.

It provides fast execution as it is It provides slow execution as it is


known at the compile time. known at the run time.

It is less flexible as mainly all the It is more flexible as all the things
things execute at the compile time. execute at the run time.

You might also like