You are on page 1of 5

1

Name:- Haider Ali

Roll no:-003

Semester:-2nd

Program:-BS-IT

Subject:-OOP

Contents
Polymorphism............................................................................................................................2

For example................................................................................................................................2

Static Polymorphism..................................................................................................................2

Function Overloading.................................................................................................................2

Operator Overloading.................................................................................................................2

Code Example............................................................................................................................3

Dynamic polymorphism.............................................................................................................3

Function Overriding...................................................................................................................3

Code Example............................................................................................................................4
2
3

Polymorphism
Polymorphism is the ability of something to be displayed in multiple forms. In programming
context, polymorphism means that some programming code or functionality or objects
behave differently in different contexts or convey messages in different forms. Polymorphism
occurs when there is a hierarchy of classes and they are related by inheritance.

For example
Let’s take a real life scenario; a person at the same time can perform several duties as per
demand, in the particular scenario. Such as, a man at a same time can serve as a father, as a
husband, as a son, and as an employee. So, single person possess different behaviors in
respective situations. This is the real life example of polymorphism. It is one of the important
features of OOP (Object Oriented Programming).

Static Polymorphism
This type of polymorphism is also known as static polymorphism and is achieved by
overloading a function, method or an operator.

Function Overloading
When multiple functions are used at different places with same name but different parameters
then these functions are known as overloaded function. Functions can be overloaded in two
ways:

1) By change in number of arguments

2) By change in type of arguments

Operator Overloading
The second method of compile time polymorphism is operator overloading. For example, we
can make the operator (‘+’) for string class to concatenate two strings.  The general concept
regarding “+” operator is that it is an addition operator whose task is to sum up two operands.
But here in polymorphism, it will be used for a different purpose.
4

Code Example
#include <iostream>
Using namespace std;
Class addition{
Public:
int sum_number (int number1, int number2)
{return number1+number2;}
int sum_number (int number1, int number2, int number3)
{return number1+number2+ number3;}
};
int main()
{
addition obj1;
cout<<”output or result:”<<obj1.sum_number(10,20)<<endl;
cout<<”output or result:”<<obj1.sum_number(10,20,33)<<endl;
return 0;
}

Dynamic polymorphism
The second type of polymorphism is runtime polymorphism. It can be achieved by using
Function Overriding or method overriding i.e. the specific function to call will be determined
at runtime based on object’s dynamic type. Generally in C++, what virtual functions provide
is called runtime polymorphism.

Function Overriding
Function overriding is giving another definition to an existing method with same parameters
or we can say that a method has same prototype in base as well as derived class.

For example: when an inherited class in C++ has a different definition for one of functions of
the base class then here the function of base class is said to be overridden.
5

Code Example
#include <iostream>
Using namespace std;
Class super
{
Public:
Void display();
{
cout<<”Super class function”;
}
};
class sub : public super{
Void display();
{
cout<<”Sub class function”;
};
int main()
{
super obj;
obj.display();
sub obj1;
obj1.display();
Return 0;
}

You might also like