12438_16virtual functions & polymorphism

Upload: ankesh-kunwar

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    1/18

    Virtual Functions&

    Polymorphism

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    2/18

    Polymorphism

    Combination of poly & morphism

    many forms

    Allows defining various forms of a singlefunction that can be shared by various objects to

    perform the identical operations.

    Implemented at compile & run time.

    function overloading virtual functions

    & operator overloading

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    3/18

    Contd

    Binding is the process of tying the function call to

    function definition.

    When binding happens at compile time

    earlybinding.

    When binding happens at run time late

    binding.

    Rules for virtual functions P. No. 281 (Title Book)

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    4/18

    Static Binding

    When the type of a formal parameter is a parent class, the

    argument used can be:

    the same type as the formal parameter,

    or,

    any derived class type.

    Static binding is the compile-time determination of which

    function to call for a particular object based on the type of the

    formal parameter

    When pass-by-value is used, static binding occurs

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    5/18

    5

    Can we do better?

    void Print (Time someTime ) //pass an object by value

    {

    cout

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    6/18

    Dynamic Binding

    Is the run-time determination of which function to call for a

    particular object of a derived class based on the type of the

    argument

    Declaring a member function to be virtual instructs the

    compiler to generate code that guarantees dynamic binding

    Dynamic binding requires pass-by-reference

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    7/18

    Polymorphism Summary:

    When you use virtual functions, compiler store

    additional information about the types of object

    available and created

    Polymorphism is supported at this additionaloverhead

    Important :

    virtual functions work only with pointers/references Not with objects even if the function is virtual

    If a class declares any virtual methods, the destructor of

    the class should be declared as virtual as well.

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    8/18

    Contd

    To implement run time polymorphism, you need

    to make a function virtual & access the virtual

    function throughpointerto the base class.

    A function in a class is preceded by virtual

    becomes virtual function.

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    9/18

    Virtual Functions

    A member function whose function declaration is

    preceded by virtual keyword virtual

    function.

    These functions are defined in a base class in the

    public section & they provide a mechanism by

    which the derived classes can override it.

    virtual member function in any derived class

    this is called overriding

    understand the contrast with overloading

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    10/18

    Syntaxclass sampleclass{

    private:

    ::::::::::: //data members of the class

    public:

    ::::::::::: //function members of the class

    virtual ReturnType FunctionName (arg)

    {:::::::::::: //body of the virtual function

    }

    };

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    11/18

    Example

    class A {

    public:

    virtual void f() { cout

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    12/18

    Output

    int main() {

    B b; C c;

    A* pa1 = &b;

    A* pa2 = &c;

    // b.f();

    pa1->f();pa2->f();

    }

    Outputs:

    Class A

    Class C

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    13/18

    A virtual function without a body purevirtualfunctionor do nothing function.

    A class with at least one pure virtual function

    abstractclass.Syntax:- Pure Virtual Function

    class sampleclass

    { private:

    ::::::::::: //data members of the classpublic:

    ::::::::::: //function members of the class

    virtual ReturnType FunctionName (arg) = 0;

    };

    Pure Virtual Functions &

    Abstract Classes

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    14/18

    Some classes exist logically but not physically. Example : Shape

    Shape s;// Legal but silly..!! : Shapelessshape

    Shape makes sense only as a base of some classes derived

    from it. Serves as a category Hence instantiation of such a class must be prevented

    class Shape //Abstract{

    public ://Pure virtual Functionvirtual void draw() = 0;

    }

    A class with one or more pure

    virtual functions is an AbstractClass. Objects of abstract class

    cant be created

    Shape s;// error : variable of an abstract class

    Pure Virtual Functions &

    Abstract Classes

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    15/18

    Example

    Shape

    virtual void draw()

    Circle

    public void draw()

    Triangle

    public void draw()

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    16/18

    A pure virtual function not defined in the derived

    class remains a pure virtual function. Hence derived class also becomes abstract

    class Circle : public Shape {//No draw() - Abstractpublic :void print(){cout

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    17/18

    Virtual Destructors

    We can have virtual destructors but not virtual

    constructors.

    Problem: If base-class pointer to a derived object is deleted, the base-class

    destructor will act on the object

    Solution: Declare a virtual base-class destructor

    Now, the appropriate destructor will be called

  • 7/29/2019 12438_16virtual Functions & Polymorphism

    18/18

    Thank You!!!