Professional Documents
Culture Documents
POLYMORPHISM
Polymorphism is one of the crucial features of OOP, which simply means one name,
multiple forms.
Polymorphism
Compile
Time/Early Run Time/
Binding Late Binding
Function Operator
Overloading Overloading Virtual Functions/Function
Overriding
What is polymorphism
Polymorphism means ability to take more than one form.
An operation may exhibit different behaviour in different instances.
The behavior depends upon the types of data used in the
operation.
Eg. function overloading, function overriding, virtual function,operator
overloading
There are two type of polymorphism
1.Compile time polymorphism
2.Run time polymorphism
Compile time polymorphism
It means that an object is bounded to its function call at
compile time that is linking to function call to function
definition is done at compile time.
Function which is being get called is known before
This can be implemented without using pointer
This is known as early binding or static binding.
Example: function overloading, operator overloading
Run Time Polymorphism
It means that an object is bounded to its function call at
run time that is linking to function call to function
definition is done at run time.
Function which is being get called is unknown until
appropriate function selection is done.
This can be implemented using pointers and virtual
functions.
This is known as late binding or dynamic binding.
Example: virtual function
Compile Time Polymorphism Run Time Polymorphism
This does not require pointer to This require to use pointer to object
object
1. Create a class that defines the data type that is to be used in the
overloading operation.
2. Declare the operator function operator op() in the public part of
the class(it may be either friend function or normal member
function).
3. Define the operator function to implement the required operation.
Runtime polymorphism – Virtual Functions
We use the pointer to base class to refer to all derived
class objects. But, we just observed that a base pointer,
even when it is made to contain the address of a derived
class, always execute the function in the base class.
The compiler simply ignores the contents of the pointer
and choose the member function that matches the type of
the pointer.
To achieve polymorphism in this case we use concept of
virtual function.
Virtual Functions
Virtual functions are used to achieve run time polymorphism.
When we use the same function name in both base and
derived classes, the function in base class is declared as
virtual using virtual keyword.
When a function is made virtual, C++ determines which
function to use at run time base don the type of object
pointed to by the base pointer, rather than the type of the
pointer
EXAMPLE:
class A
{
int x;
public:
void show() {……}//show in base class
};
class B:public A
{
int y;
public:
But using base pointer, we can access only those members which are inherited from base, not the
members originally belong to derived class.
In case if base and derived got same name functions/members, any reference using base pointer
always access base class member.
Rules for Virtual functions
Pure Virtual function
Pure virtual function is a function A pure virtual function is declared by
declared in a base class that has no assigning 0 in the declaration.
definition relative to the base class.
class Test {
‘Also called as Do-nothing functions’
// Data members of class
Class containing pure virtual functions
public:
cannot be used to declare its own
// Pure Virtual Function
objects – Abstract base classes.
virtual void show() = 0;
A class is abstract if it has at least one
pure virtual function. };