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

Polymorphism

The document discusses runtime polymorphism in object-oriented programming. Runtime polymorphism allows the same function to behave differently depending on the type of object it is being called on. It is implemented through virtual functions, where the function implementation is looked up at runtime based on the actual object type.

Uploaded by

Niroj Pani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Polymorphism

The document discusses runtime polymorphism in object-oriented programming. Runtime polymorphism allows the same function to behave differently depending on the type of object it is being called on. It is implemented through virtual functions, where the function implementation is looked up at runtime based on the actual object type.

Uploaded by

Niroj Pani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

RUNTIME POLYMORPHISM

1
Polymorphism – An Introduction

◼ The term Polymorphism has been derived from the


Greek word Polu-morphous which means “the quality
or state of being able to assume different forms”

◼ In OO concept it means one function / operator can


do different kind of things & hence has different
meanings in different situations.
EX- The operator + has a different meaning in the expression
2 + 3 (add two integers) than in 1.7 + 3.3 (add two floating point
numbers)

◼ Polymorphism is build upon Inheritance


2
Types of Polymorphism

Polymorphism

Compile time Run time


polymorphism / Static Binding polymorphism / Dynamic binding

Function Operator
Virtual Functions
Overloading Overloading

3
Run time Polymorphism – An Example
class shape
{
shape area() public:
void area();
};

class rectangle : public shape


{
float length, breadth;
public:
rectangle circle void area(){………}
};
triangle
class circle : public shape
{
float radius;
square public:
void area(){………}
};

We want the same function area() should calculate the area of all the
shapes depending upon the situation. 4
Run time Polymorphism – An Example
class shape { int main()
public: {
void area(); shape *sp;
};

class rectangle : public shape sp = new rectangle;


{ sp->area();
float length, breadth; }
public:
void area(){
cout << “area=“ ;
cout << length*breadth;
}
Which function is called?
}; -The function in
class circle : public shape
shape
{ is called.
float radius;
public:
To avoid this area() in
void area(){
cout << “area=” << 2*3.14*radius; class shape is
} declared
}; as a virtual function
5
Virtual functions
// Program: ex4.cpp class circle : public shape
# include<iostream.h> {
using namespace std; float radius;
class shape public:
{ circle :: circle(float r)
public: {radious = r;}
virtual void area(); void area()
}; {
cout << “area=”;
class rectangle : public shape { cout << 2*3.14*radius;
float length, breadth; }
public: };
rectangle :: rectangle(float
l, float b) int main()
{
{
shape *sp;
length = l; breadth = b;
} sp = new rectangle(5.0, 3.0);
void area()
sp->area();
{
sp = new circle(2.0);
cout << “area=“ ;
cout << length*breadth; sp->area();
} return 0;
6
}; }
Pure virtual function

◼ A pure virtual function is declared in the base class


but not implemented. Implementations are given in
specific derived class.

◼ Declare
virtual function prototype = 0;
Ex: virtual area()=0;

You might also like