Professional Documents
Culture Documents
Object Oriented
Programing [OOP]
BS Software Engineering
2nd Semester
Qasid Abbas
Contents
Polymorphism............................................................................................................................2
For example................................................................................................................................2
Function Overloading.................................................................................................................2
Operator Overloading.................................................................................................................3
Output.........................................................................................................................................3
2) Runtime Polymorphism.........................................................................................................3
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.
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:
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.
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.
Return 0;
}
5
Output
Super Class Function
Sub Class Function