You are on page 1of 20

Run Time Polymorphism

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

It means that an object is bounded It means that an object is bounded


to its function call at compile time to its function call at run time that is
that is linking to function call to linking to function call to function
function definition is done at definition is done at run time.
compile time.
Function which is being get called Function which is being get called
is known before is unknown until appropriate
function selection is done.

This does not require pointer to This require to use pointer to object
object

Function calls are faster Function calls execution is slower


This is known as earlier binding or This is known as late binding or
static binding. dynamic binding.
Eg. Function overloading,operator Eg. Virtual Function
overloading

Compile time polymorphism can be implemented using

1.Function overloading

2.Operator overloading

Function Overloading

This means that we can use the same function name to
create function that performs variety of different tasks.

We can design multiple function with one function name but
different argument lists.

The function would perform different operation depending on
the argument list in the function call.

The correct function to be invoked is determined by
checking the number and type of the arguments but
not on the function type.

Eg.

void add(int a, int b);

int add(int x, int y);

int add(int x, int y, int z);

void add(float p, float q);

Operator Overloading

This means that giving special meaning to an operator is
known as operator overloading.

operator overloading provides a flexible option for the
creation of new definitions for most of the C++ operator.

We can overload all the C++ operator except the following.

Class member access operator(.,.*)

Scope resolution operator(::)

Size operator(sizeof)

Conditional operator(?:)

To define additional task to an operator(operator
overloading) we must specify what it means in relation to
the class to which the operator is applied.

This id done with the help of special function called
operator function.

The general form of operator function is:

return_type class_name::operator(op-arglist)

{

Function body;

}

Rules for Operator Overloading

Operator function must be either member function(non-
static) or friend function

A friend function will have only one argument for
unary operator and two for binary operator.

A member function has no argument for unary
operator and only one for binary operator.

The object used to invoke the member function is
passed implicitly.

Argument may be passed either by value or by
reference.

The process of overloading involves the following steps:

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:

void show() {….}//show in derived class


};
POINTERS TO DERIVED CLASSES
A base class pointer can point to objects of both the base and derived class.

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. };

You might also like