Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

POLYMORPHISM

The process of representing one Form in multiple forms is


known as Polymorphism.
Here one form represent original form or original method
always resides in base class and multiple forms represents
overridden method which resides in derived classes.
Polymorphism is achieved by function overloading and
function overriding.

Polymorphism is derived from 2 Greek words: poly and


morphs. The word "poly" means many and morphs means
forms.
REAL LIFE EXAMPLE OF
POLYMORPHISM
Suppose if you are in class room that time you behave
like a student, when you are in market at that time you
behave like a customer, when you at your home at that
time you behave like a son or daughter, Here one person
have different-different behaviors.
TYPE OF POLYMORPHISM

Polymorphism

Static Dynamic

Function Operator Virtual


Overloading Overloading Functions
TYPE OF POLYMORPHISM

 Static polymorphism is also known as early binding and compile-time


polymorphism. In static polymorphism memory will be allocated at compile-time.
Dynamic polymorphism is also known as late binding and run-time polymorphism.
In dynamic polymorphism memory will be allocated at run-time.
COMPILE TIME OVERLOADING

• FUNCTION / METHOD OVERLOADING


METHOD/FUNCTION
OVERLOADING
Function overloading is a form of static polymorphism where at least two
functions can have a similar name with different arguments/parameters.

Whenever same method name is existing multiple times in the same class with
different number of parameter or different order of parameters or different
types of parameters is known as method/function overloading.

A compiler calls correct function out of the others on the basis of their number of
parameters or datatypes in arguments.
FUNCTION OVERLOADING
• Conditions for function overloading are:-
• Functions to be overloaded must have the same name.
• All functions must have different arguments( either a different number of
parameters or different type of parameters ).
• In next example method "sum()" is present in addition class with same name but
with different signature or arguments.
EXAMPLE
#include<iostream>
int main()
{
using namespace std;
class shape shape s;
{ cout<<"area of square="<<s.area(10)<<endl;
cout<<"area of rectangle="<<s.area(10,20)<<endl;
public: return 0;
int area(int dim)
{ }
return dim*dim;
}
int area(int len,int wdt)
{
return len*wdt;
}

};
#include <iostream>
using namespace std;
class rectangle
{ int main()
public:
void printarea(int x, int y) {
{ rectangle t;
cout << x * y << endl;
} t.printarea(2,4);
void printarea(int x) t.printarea(2,5.1);
{
cout << x * x << endl; t.printarea(10);
} t.printarea(2.3);
void printarea(int x, double y)
{ return 0;
cout << x * y << endl; }
}
void printarea(double x)
{
cout << x * x << endl;
}
};
RUNTIME
POLYMORPHISM
• Function overriding is an example of Runtime or dynamic polymorphism.
• Function overriding enables us to have an equivalent function in child class
which is already defined in the parent class.
• 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, thats the reason it is called runtime
polymorphism.
FUNCTION OVERRIDING
• Conditions for function overriding
• Functions of both parent and child class must have the
same name.
• Functions must have the same argument list and return
type.
• A function declared static cannot be overridden.
• If a function cannot be inherited, it cannot be overridden.
FUNCTION OVERRIDING
• 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, thats the reason it is
called runtime polymorphism.
• Write a C++ program that accepts the dimensions of a
rectangle and a rhombus. The program should then use an
overloaded function named area to determine area of the
respective figures. Output the dimensions and areas of the
figures.
Write a C++ program that will carry out the following:
• define a class named circular that has data members named radius and height, a member
function named set which is used to initialize the value of length and height and a function named
volume,
• implements a derived class from circular named cylinder whose radius and height are 14 cm and
10 cm respectively;
• implements a derived class from circular named cone whose radius and height are 14cm and 5
cm respectively;

• outputs the volume for the cylinder and cone.


• Exercise
write a C++ program that would define an abstract base class named bill with data members named
units and standingfees, a member function named init (for initializing standingfee and units) and a
polymorphic function for determining the consumption costs. The program should implement the
polymorphic function in two derived classes named water and electricity based on the following
information:
• The standing fee for water and electricity is 70 and 250 respectively;
• 150 unites consumed for both water and electricity;
• Consumption cost for water=standingfees+(units*100);
• Consumption cost for electricity=standingfees+(units*3);
The program should output the consumption costs for water and electricity.

You might also like