Polymorphism
Polymorphism means “many forms”.
Polymorphism means ability to take more than one form.
Polymorphism is the ability to use an operator or function in
different ways. Polymorphism gives different meanings or
functions to the operators or functions. Poly, referring too many,
signifies the many uses of these operators and functions. A single
function usage or an operator functioning in many ways can be
called polymorphism. Polymorphism refers to codes, operations or
objects that behave differently in different contexts.
Types of Polymorphism:
• C++ provides two different types of polymorphism.
• Run-time
• Compile-time
Compile Time Polymorphism
• Compile time polymorphism is also known as static binding or early
binding.
Function overloading and Operator overloading are the example of
compile time polymorphism.
It is called compile time polymorphism because which version of
function to invoke is determined by the compiler at compile time
based on number and types of the argument.
Function Overloading
In C++, two or more functions can share the same name as
long as their parameter declarations are different.
In this situation, the functions that share the same name are
said to be overloaded, and the process is referred to as
function overloading.
DEFINITION
It is the process of using the same name for two or more
functions.
The secret to overloading is that each redefinition of the
function must use either
• different types of parameters
• different number of parameters.
• The key to function overloading is a function’s argument list.
• A function’s argument list is known as its signature.
Different types of
arguments
Different number of
parameter
void print(int a); void area(float r);
void print (double b); void area(float l, float b);
void print(char c); void area(float a, float b,
float c);
If two function have same number and types
of arguments in the same order, they are said
to have the same signature.
There is no importance to their variable
names.
• void print(int x, float y);
• void print(int p, float q);
are said to have same signature.
Operator Overloading
• It is one type of Static Polymorphism.
• C++ has about 45 operators. These operators are defined for the
fundamental data types (int, float etc).
• While defining a class, we are actually creating a new datatype.
• Most of the C++ operators can be overloaded to apply to our new
class datatype
• Operator overloading is the concept of giving new meaning to an
existing C++ operator.
• When overloaded, the operators are implemented as functions using
the operator keyword.
• For example, the syntax of overloading the addition operator “+”
would be operator+().
• One of the nice features of C++ is that you can give special meanings
to operators, when they are used with user-defined classes. This is
called operator overloading.
• Arithmetic operators overloaded using member function of a class
must have one argument as object of class.
• Arithmetic operators overloaded using friend function of a class must
have two argument
 Can perform variety of
functions with same function
name.
 No need to remember name of
many functions.
 Functions should have
different argument lists.
 Member functions cannot be
overloaded solely on the basis
of one being static and the
other being non static.
Runtime Polymorphism
• When the target object and/or the invoked method is not known at
compile time, so the binding is delayed till runtime. This is called
runtime binding or late binding or runtime Polymorphism.
Compile time Polymorphism Run time Polymorphism
In Compile time Polymorphism, call is resolved
by the compiler.
In Run time Polymorphism, call is not resolved
by the compiler.
It is also known as Static binding, Early
binding and overloading as well.
It is also known as Dynamic binding, Late
binding and overriding as well.
Overloading is compile time polymorphism
where more than one methods share the same
name with different parameters or signature
and different return type.
Overriding is run time polymorphism having
same method with same parameters or
signature, but associated in a class & its
subclass.
It is achieved by function overloading
and operatoroverloading.
It is achieved by virtual
functions and pointers.
It provides fast execution because known
early at compile time.
It provides slow execution as compare to
early binding because it is known at runtime.
Compile time polymorphism is less flexible as
all things execute at compile time.
Run time polymorphism is more flexible as all
things execute at run time.
Polymorphism and its types

Polymorphism and its types

  • 1.
    Polymorphism Polymorphism means “manyforms”. Polymorphism means ability to take more than one form. Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring too many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
  • 2.
    Types of Polymorphism: •C++ provides two different types of polymorphism. • Run-time • Compile-time
  • 3.
    Compile Time Polymorphism •Compile time polymorphism is also known as static binding or early binding. Function overloading and Operator overloading are the example of compile time polymorphism. It is called compile time polymorphism because which version of function to invoke is determined by the compiler at compile time based on number and types of the argument.
  • 6.
    Function Overloading In C++,two or more functions can share the same name as long as their parameter declarations are different. In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading. DEFINITION It is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either • different types of parameters • different number of parameters.
  • 7.
    • The keyto function overloading is a function’s argument list. • A function’s argument list is known as its signature. Different types of arguments Different number of parameter void print(int a); void area(float r); void print (double b); void area(float l, float b); void print(char c); void area(float a, float b, float c);
  • 8.
    If two functionhave same number and types of arguments in the same order, they are said to have the same signature. There is no importance to their variable names. • void print(int x, float y); • void print(int p, float q); are said to have same signature.
  • 9.
    Operator Overloading • Itis one type of Static Polymorphism. • C++ has about 45 operators. These operators are defined for the fundamental data types (int, float etc). • While defining a class, we are actually creating a new datatype. • Most of the C++ operators can be overloaded to apply to our new class datatype
  • 10.
    • Operator overloadingis the concept of giving new meaning to an existing C++ operator. • When overloaded, the operators are implemented as functions using the operator keyword. • For example, the syntax of overloading the addition operator “+” would be operator+(). • One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading.
  • 11.
    • Arithmetic operatorsoverloaded using member function of a class must have one argument as object of class. • Arithmetic operators overloaded using friend function of a class must have two argument
  • 12.
     Can performvariety of functions with same function name.  No need to remember name of many functions.
  • 13.
     Functions shouldhave different argument lists.  Member functions cannot be overloaded solely on the basis of one being static and the other being non static.
  • 14.
    Runtime Polymorphism • Whenthe target object and/or the invoked method is not known at compile time, so the binding is delayed till runtime. This is called runtime binding or late binding or runtime Polymorphism.
  • 15.
    Compile time PolymorphismRun time Polymorphism In Compile time Polymorphism, call is resolved by the compiler. In Run time Polymorphism, call is not resolved by the compiler. It is also known as Static binding, Early binding and overloading as well. It is also known as Dynamic binding, Late binding and overriding as well. Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type. Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass. It is achieved by function overloading and operatoroverloading. It is achieved by virtual functions and pointers. It provides fast execution because known early at compile time. It provides slow execution as compare to early binding because it is known at runtime. Compile time polymorphism is less flexible as all things execute at compile time. Run time polymorphism is more flexible as all things execute at run time.