Programming Using C++ Session 14

Embed Size (px)

Citation preview

  • 8/13/2019 Programming Using C++ Session 14

    1/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 1 of 15

    Objectives

    In this lesson, you will learn to:

    Implement the concept of bindingUse virtual functions

    Use pure virtual functions to create abstract classes

    Implement dynamic polymorphism by using latebinding

  • 8/13/2019 Programming Using C++ Session 14

    2/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 2 of 15

    Dynamic Polymorphism

    Refers to any entity changing its form, depending oncircumstances

  • 8/13/2019 Programming Using C++ Session 14

    3/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 3 of 15

    Binding

    Is the process of associating a function with a class byidentifying the type of the object or pointer that is usedto invoke the function

  • 8/13/2019 Programming Using C++ Session 14

    4/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 4 of 15

    Binding (Contd.)

    Example:

    calc_net_salary()

    calc_net_salary() calc_net_salary()

    Trainee Class Confirmed Class

    Employee class

  • 8/13/2019 Programming Using C++ Session 14

    5/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 5 of 15

    Dynamic Binding

    Is done during runtime

    Is also called late binding

  • 8/13/2019 Programming Using C++ Session 14

    6/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 6 of 15

    Virtual Function

    Is a function that is declared as virtual in a base classand is redefined by a derived class

  • 8/13/2019 Programming Using C++ Session 14

    7/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 7 of 15

    Using Virtual FunctionsExample:class Employee

    {..virtual int calc_net_salary();

    ..};class Contract:public Employee

    {..

  • 8/13/2019 Programming Using C++ Session 14

    8/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 8 of 15

    Using Virtual Functions (Contd.)int calc_net_salary();.

    .

    .};class Direct_Contract: public Contract

    {..int calc_net_salary();

    .

    .};

  • 8/13/2019 Programming Using C++ Session 14

    9/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 9 of 15

    Pure Virtual Function

    Is a function without a body

    Is created by adding the notation =0 to the virtualfunction declaration

    Example:

    virtual int calc_net_salary()=0;

  • 8/13/2019 Programming Using C++ Session 14

    10/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 10 of 15

    Abstract Class

    Is a class containing one or more pure virtualfunctions

    Is used as a base class for deriving specific classesof the same kind

  • 8/13/2019 Programming Using C++ Session 14

    11/15

  • 8/13/2019 Programming Using C++ Session 14

    12/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 12 of 15

    Static vs Dynamic Polymorphism (Contd.)

    Dynamic polymorphism

    Is considered more flexibleIs based on overriding principles, which, therefore,is purely class scope and is based on inheritance

  • 8/13/2019 Programming Using C++ Session 14

    13/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 13 of 15

    Problem Statement 10.D.1

    Create an application that:

    Accepts customer details Accepts dealer details

    Displays customer details

    Displays dealer details

  • 8/13/2019 Programming Using C++ Session 14

    14/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 14 of 15

    Summary

    In this lesson, you learned that:

    The instances of an abstract class cannot be createdbut pointers to it can be created

    A function exhibits dynamic polymorphism when itexists in more than one form, and calls to its various

    forms are resolved dynamically when the program isexecuted

    Static binding is considered to be more efficient whiledynamic binding is more flexible

  • 8/13/2019 Programming Using C++ Session 14

    15/15

    Dynamic Polymorphism

    NIIT Programming Using C++/Session 14/Slide 15 of 15

    Summary (Contd.)

    Dynamic polymorphism refers to an entity changing itsform, depending on circumstances

    The virtual keyword is used to declare a function asvirtual

    Virtual functions that have no body are called pure

    virtual functions A class containing one or more pure virtual functionsis called an abstract class