You are on page 1of 6

MINHAJ UNIVERSITY LAHORE

Object Oriented
Programing [OOP]
BS Software Engineering
2nd Semester

Qasid Abbas

Sir Muhammad Akmal


1

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

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

1) Compile Time Polymorphism................................................................................................2

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

Operator Overloading.................................................................................................................3

Example of Compile Time Polymorphism................................................................................3

Output.........................................................................................................................................3

2) Runtime Polymorphism.........................................................................................................3

Example of Runtime Polymorphism..........................................................................................4

Output.........................................................................................................................................5
2

Polymorphism
Polymorphism is a combination of two Greek word poly which means many and
Morphism means forms. It is an important feature of C++ language. In C++, polymorphism
causes a member function to behave differently based on the object that calls/invokes it. It
occurs when you have a hierarchy of classes related through inheritance. Suppose we have
the function makeSound (). When a cat calls this function, it will produce the meow sound.
When a cow invokes the same function, it will provide the moow sound. Polymorphism
consists of two types one is Static (compile time) polymorphism and the second is Dynamic
(run time) polymorphism.

For example
A real-life example of polymorphism, a person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an employee. So the same
person possesses different behavior in different situations. This is called polymorphism.

1) Compile Time Polymorphism


This type of polymorphism is also known as static polymorphism and is achieved by
overloading a function, method or an operator. It is the linking of function with an object
during compile time of program. Static polymorphism is also called static binding. It also has
two types:

(1) Function overloading

(2) Operator overloading

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


3

Operator Overloading
C++ also provides 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.

Example of Compile Time Polymorphism


#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;
}

Output
Output or result: 30
Output or result: 66

2) Runtime Polymorphism
Function overriding is an example of Runtime polymorphism.
4

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.

Example of Runtime Polymorphism


#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;

}
5

Output
Super Class Function
Sub Class Function

You might also like