oops virtual functions

Upload: arpit-singh

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Oops Virtual Functions

    1/15

    Virtual Functions

    Presenting By:Arpit SinghB.Tech (I.T)090102014Objected Oriented Programming

  • 8/3/2019 Oops Virtual Functions

    2/15

    Introduction

    A virtual function is a member function that is declared within a base classand redefined by a derived class. To create virtual function, precede thefunctions declaration in the base class with the keyword virtual. When aclass containing virtual function is inherited, the derived class redefines thevirtual function to suit its own needs.

    Base class pointer can point to derived class object. In this case, using baseclass pointer if we call some function which is in both classes, then baseclass function is invoked. But if we want to invoke derived class functionusing base class pointer, it can be achieved by defining the function as virtualin base class, this is how virtual functions support runtime polymorphism.

    Properties of a virtual function:A member function of a class.Declared with virtual keyword.Usually has a different functionality in the derived class.A function call is resolved dynamically at run-time.

  • 8/3/2019 Oops Virtual Functions

    3/15

    How does a Virtual Function work?

    Whenever a program has a virtual function declared, a v - tableis constructed for the class.

    The v-table consists of addresses to the virtual functions for classes that contain one or more virtualfunctions. The object of the class containing the virtual function contains a virtual pointer thatpoints to the base address of the virtual table in memory.

    Whenever there is a virtual function call, the v-table is used to resolve to the function address. Anobject of the class that contains one or more virtual functions contains a virtual pointer called the vptrat the very beginning of the object in the memory. Hence the size of the object in this case increasesby the size of the pointer.

    This vptr contains the base address of the virtual table in memory. Note that virtualtables are class specific, i.e., there is only one virtual table for a class irrespective of the numberof virtual functions it contains. This virtual table in turn contains the base addresses of one ormore virtual functions of the class.

    At the time when a virtual function is called on an object, thevptr of that object provides the base address of the virtual table for that class in memory. Thistable is used to resolve the function call as it contains the addresses of all the virtual functions ofthat class. This is how dynamic bindingis resolved during a virtual function call.

  • 8/3/2019 Oops Virtual Functions

    4/15

    V-Table

    v-tables are used for virtual functions. Its a short form for Virtual FunctionTable.

    It's a static table created by the compiler. Compiler creates a static table perclass and the data consists on pointers to the virtual function definitions. They

    are automatically initialised by the compiler's constructor code.

    Since virtual function pointers are stored in each instance, the compiler isenabled to call the correct virtual function at runtime

  • 8/3/2019 Oops Virtual Functions

    5/15

    What is Binding?

    Binding refers to the act of associating an object or a class with its member.

    If we can call a function fn() on an object o of a class c, we say that theobject o is binded with the function fn().This happens at compile time and isknown as static or compile - time binding.

    The calls to the virtual member functions are resolved during run-time. Thismechanism is known as dynamic binding. The most prominent reason why avirtual function will be used is to have a different functionality in the derivedclass.

    The difference between a non-virtual member function and avirtual member function is, the non-virtual member functions are resolved atcompile time.

  • 8/3/2019 Oops Virtual Functions

    6/15

    Exampleclass base // Base class for C++ virtual function example

    {

    public :

    virtual void show() // virtual function for C++ virtual function

    example

    {

    cout show();

    getch();

    }

    Output:

  • 8/3/2019 Oops Virtual Functions

    7/15

  • 8/3/2019 Oops Virtual Functions

    8/15

    Pure Virtual Function

    A pure virtual function is a function which has no definition in the baseclass. Its definition lies only in the derived class ie it is compulsory forthe derived class to provide definition of a pure virtual function. Sincethere is no definition in the base class, these functions can be equated

    to zero. The general form of pure virtual function is:

    virtual type func-name(parameter-list) = 0;

    The base class with pure virtual function can't be instantiated sincethere is no definition of the function in the base class. It is necessary

    for the derived class to override pure virtual function. This type ofclass with one or more pure virtual function is called abstract classwhich can't be instantiated, it can only be inherited.

  • 8/3/2019 Oops Virtual Functions

    9/15

    class shape // Base class for C++ virtual function example

    {

    public:

    virtualvoid area() = ;0

    };

    class square : public shape

    {

    public:

    void area()

    {

    cout

  • 8/3/2019 Oops Virtual Functions

    10/15

    Example

    Output:

    void main()

    {

    shape *x;

    x = new square();

    x->area();

    x = new rectangle();

    x->area();getch();

    }

  • 8/3/2019 Oops Virtual Functions

    11/15

  • 8/3/2019 Oops Virtual Functions

    12/15

    Virtual Destructor

    If the destructor in the base class is not made virtual, then an object that mighthave been declared of type base class and instance of child class wouldsimply call the base class destructor without calling the derived classdestructor.

    Hence, by making the destructor in the base class virtual, we ensure that thederived class destructor gets called before the base class destructor

  • 8/3/2019 Oops Virtual Functions

    13/15

    Exampleclass base{

    public :

    base()

    {

    cout

  • 8/3/2019 Oops Virtual Functions

    14/15

    Output:

    Examplevoid main()

    {

    base *ptr =new derived();

    // some code

    delete ptr;

    getch();

    }

  • 8/3/2019 Oops Virtual Functions

    15/15