polymorphism concept

Post on 30-May-2018

218 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 8/14/2019 polymorphism concept

    1/13

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

    A PRESENATION

    BY

    Pawan Sharma

  • 8/14/2019 polymorphism concept

    2/13

    PLOYMORPHISMPLOYMORPHISM

    POLY+MORPHISM[GREEK]=MANY+FOPOLY+MORPHISM[GREEK]=MANY+FO

    RMRM

    Polymorphism means ability to takePolymorphism means ability to take

    more than one form.more than one form.

    Polymorphism refers to codes,Polymorphism refers to codes,

    operations or objects that behaveoperations or objects that behavedifferently in different contexts.differently in different contexts.

  • 8/14/2019 polymorphism concept

    3/13

    Polymorphism

    Compile time

    Function

    overloading

    Operator

    overloading

    Runtime

    Virtual

    function

    TYPES OF POLYMORPHISMTYPES OF POLYMORPHISM

  • 8/14/2019 polymorphism concept

    4/13

    Company

    LOGO

    www.company.com

    COMPILE TIME POLYMORPHISM During compilation time the C++

    compiler determines which

    function is used based on the

    parameters passed to the

    function or the function returntype.

    The compiler than substitute the

    correct function for each

    evocation.

    This compiler based substitution

    are called compiler timepolymorphism

    it is also called as early binding

    or static binding.

    Advantages Grater efficiency can be

    achieved with early binding

    Function calls are faster in thiscase which will increase

    execution speed.

  • 8/14/2019 polymorphism concept

    5/13

    FUNCTION OVERLOADING

    Using a same function name to perform different types oftasks is known as function overloading.

    Example:-

    Building

    office house cottage

  • 8/14/2019 polymorphism concept

    6/13

    Demo Example of functionoverloading

    Declarationint fun();{

    Return (hello);}

    Int fun(int a);{

    return(a*2);}Int fun(int a, float b);

    {return(a+b);

    }

    Function callsFun(); //executed null parameters function//Cout

  • 8/14/2019 polymorphism concept

    7/13

    About function overloading

    Function overloading is possible in any function.functionwhich is declared in same class or outside of the class.

    Function should differ either in number of parameters ordata type of parameters.ex int fun(int a,int b), int fun(int

    a,float b).

  • 8/14/2019 polymorphism concept

    8/13

    OPERATOR OVERLOADINGOPERATOR OVERLOADING

    Why there is necessity of overloading the operator?Why there is necessity of overloading the operator? c++ provide many operator like +,-,*,/.

  • 8/14/2019 polymorphism concept

    9/13

    Rules for overloading operatorsRules for overloading operators

    Only existing operator can be overloaded .Only existing operator can be overloaded .

    We can not change the basic meaning of anWe can not change the basic meaning of an

    operator i.e. can not redefine the plus (+)operator i.e. can not redefine the plus (+)

    operator to subtract one value from the other.operator to subtract one value from the other.

    Some operator are not need to be overloadedSome operator are not need to be overloaded

    (&&,||).(&&,||).

    Some operator can not be overloaded(:,::,**).Some operator can not be overloaded(:,::,**).

  • 8/14/2019 polymorphism concept

    10/13

    VIRTUAL FUNCTIONVIRTUAL FUNCTION

    We know that the derive class can access the property ofWe know that the derive class can access the property ofbase class but base class object can not access thebase class but base class object can not access theproperty of derive class.property of derive class.

    But it is possible for the pointer to access the property ofBut it is possible for the pointer to access the property of

    derived class by base class object.derived class by base class object. If we want to use thisIf we want to use this base class pointerbase class pointer calls functions fromcalls functions from

    derived class so we need to declare base class function as aderived class so we need to declare base class function as avirtual function.virtual function.

    i.e. : virtual void show()=0;this is now said to be ai.e. : virtual void show()=0;this is now said to be a PurePurevirtual functionvirtual function, which does have the body. its body has to, which does have the body. its body has to

    be define in derived class.be define in derived class. The pointer of base class can calls only those functions ofThe pointer of base class can calls only those functions of

    the derived class which override certain function of thethe derived class which override certain function of thebase class.base class.

  • 8/14/2019 polymorphism concept

    11/13

    Run time polymorphism is achieved withRun time polymorphism is achieved withvirtual function.virtual function.

    When we use the same function name inWhen we use the same function name in

    both the base class and derived classes, theboth the base class and derived classes, thefunction in base class is declared as virtualfunction in base class is declared as virtualusing the keyword virtual preceding itsusing the keyword virtual preceding itsnormal declaration. When a function is madenormal declaration. When a function is madevirtual ,C++ determines which function tovirtual ,C++ determines which function touse at run-time based on the type of objectuse at run-time based on the type of objectpointed to by the base pointer,rahter thenpointed to by the base pointer,rahter thenthe type of the pointer.the type of the pointer.

    Thus making the base pointer to point toThus making the base pointer to point todifferent objects, we can execute differentdifferent objects, we can execute different

  • 8/14/2019 polymorphism concept

    12/13

    Important about the virtualImportant about the virtual

    functionfunctionThe virtual always declare in baseThe virtual always declare in base

    class.class.

    The pointer of the base class onlyThe pointer of the base class onlypoint to the virtual function(overidepoint to the virtual function(overide

    function) of base class and drivefunction) of base class and drive

    class.class.

  • 8/14/2019 polymorphism concept

    13/13>> 0 >> 1 >> 2 >> 3 >> 4 >>

    Quires? Thanks to all of you