You are on page 1of 13

A PRESENATION

BY

Pawan Sharma

>> 0 >> 1 >> 2 >> 3 >> 4 >>


PLOYMORPHISM
 POLY+MORPHISM[GREEK]=MANY+FO
RM
 Polymorphism means ability to take
more than one form.
 Polymorphism refers to codes,
operations or objects that behave
differently in different contexts.
TYPES OF POLYMORPHISM
Polymorphism

Compile time Runtime

Function Operator Virtual


overloading overloading function
Company
LOGO

COMPILE TIME POLYMORPHISM


• During compilation time the C++ • Advantages
compiler determines which
function is used based on the • Grater efficiency can be
parameters passed to the achieved with early binding
function or the function return • Function calls are faster in this
type. case which will increase
• The compiler than substitute the execution speed.
correct function for each
evocation.
• This compiler based substitution
are called compiler time
polymorphism
• it is also called as early binding
or static binding.

www.company.com
FUNCTION OVERLOADING
 Using a same function name to perform different types of
tasks is known as function overloading.
 Example:-

Building

office house cottage


Demo Example of function
overloading
 Declaration  Function calls
Fun(); //executed null parameters function//
int fun(); Cout<<“twice of number =“<<fun(5);
//executed single parameters function//
{ Cout<<“sum of number=“<<fun(5,6);
Return (hello); //executed two parameters function//
}
Int fun(int a);
{
return(a*2);
}
Int fun(int a, float b);
{
return(a+b);
}
About function overloading
 Function overloading is possible in any function.function
which is declared in same class or outside of the class.
 Function should differ either in number of parameters or
data type of parameters.ex int fun(int a,int b), int fun(int
a,float b).
OPERATOR OVERLOADING
 Why there is necessity of overloading the operator?
 c++ provide many operator like +,-,*,/.<= etc. using these operator we can perform
all mathematical task on predefine data type like int,float,long,double etc.
 If we try to add two structure variable is not possible because these basic operator
do not work on the user define variable directly. so these operator do not work on
the object ,so if we want to use these operator on the object directly then we have to
overload these operators .
 So operator overloading is make the operator
work on the object directly. it need to declare
member function to overload operator.

Syntax : return data type operator operator_symbole(-----)
Rules for overloading operators
 Only existing operator can be overloaded .
 We can not change the basic meaning of an
operator i.e. can not redefine the plus (+)
operator to subtract one value from the other.
 Some operator are not need to be overloaded
(&&,||).
 Some operator can not be overloaded(:,::,**).
VIRTUAL FUNCTION
 We know that the derive class can access the property of
base class but base class object can not access the
property of derive class.
 But it is possible for the pointer to access the property of
derived class by base class object.
 If we want to use this base class pointer calls functions from
derived class so we need to declare base class function as a
virtual function.
 i.e. : virtual void show()=0;this is now said to be a Pure
virtual function, which does have the body. its body has to
be define in derived class.
 The pointer of base class can calls only those functions of
the derived class which override certain function of the
base class.
 Run time polymorphism is achieved with
virtual function.
 When we use the same function name in
both the base class and derived classes, the
function in base class is declared as virtual
using the keyword virtual preceding its
normal declaration. When a function is made
virtual ,C++ determines which function to
use at run-time based on the type of object
pointed to by the base pointer,rahter then
the type of the pointer.
 Thus making the base pointer to point to
different objects, we can execute different
Important about the virtual
function
 The virtual always declare in base
class.
 The pointer of the base class only
point to the virtual function(overide
function) of base class and drive
class.
Quires?
• Thanks to all of you

>> 0 >> 1 >> 2 >> 3 >> 4 >>

You might also like