Virtual Functions in C++

Once again we are into a completely new topic with terminology which will be new to you. If you are new to object oriented programming, you should follow along in this tutorial very carefully because every attempt has been made to define every detail of this new and somewhat intimidating topic.

One term which must be defined is polymorphism, a rather large word that simply means similar when used in the context of object oriented programming. Objects are polymorphic if they have some similarities but are still somewhat different. We will see how it is used in the context of object oriented programming as we proceed.

We have already studied operator overloading and function overloading in this tutorial, and they are a subtle form of polymorphism since in both cases, a single entity is used to refer to two or more things. The use of virtual functions can be a great aid in programming some kinds of projects as you will see in these two chapters.

So what is a Virtual Function?

A Virtual function is a function whic is declared in base class using the keyword virtual. We write the body of virtual function in the derived classes. Its purpose is to tell the compiler that what function we would like to call on the basis of the object of derived class. C++ determines which function to call at run time on the type of object pointer to.

Declaration of a virtual function

Here in this example who(); function is called on the basis of the object. So if the object is of the base class then who(); function called is defined in base class.

16 Comments

  1. A derived class has an method which overrides its base class’s virtual method and where in the derived class overridden method has some code that accesses derived class’s variables.

    In this scenario, if a base class pointer is assigned derived calss reference(ie.. late binding) and the overridden method is called using base class pointer then only the base class implementation of the method is called but not the derived class method implemention.

  2. Virtual Method example –

  3. u can easily understanding other by this prog:-

  4. Assumption: machine is 32-bit .
    Here I am going to explain How Virtual table, Virtual pointer for Virtual functions are internally working.

    First we have understand memory layout.

    Example 1: How the class’s memory layout

    Output:

    Sobj’s Size = 8
    obj ‘s Address = 0012FF7C

    Note: Any Plane member function does not take any memory.

    Example 2: Memory Layout of Derived class

    OUTPUT:
    obj1’s Size = 8
    obj1’s Address = 0012FF78
    obj2’s Size = 12
    obj2’s Address = 0012FF6C

    Example 3: Memory layout If we have one virtual function.

    OUTPUT:

    obj’s Size = 8
    obj’s Address = 0012FF7C

    Note: Adding one virtual function in a class takes 4 Byte extra.

    Example 4: More than one Virtual function

    OUTPUT:

    obj’s Size = 8
    obj’s Address = 0012FF7C

    Note: Adding more virtual functions in a class, no extra size taking i.e. Only one machine size taking(i.e. 4 byte)

    Example 5:

    OUTPUT:
    a = 200
    b = 10

    If we Change the code as then

    // Changing a and b
    int* pInt = (int*)&obj;
    *(pInt+1) = 100; // In place of 0
    *(pInt+2) = 200; // In place of 1

    OUTPUT:
    a = 100
    b = 200

    Note: Who sits 1st place of Class : Answer is VPTR
    VPTR – 1st placed in class and rest sits after it.

    Example 6:

    OUTPUT:

    VPTR’s Address 0012FF7C
    VPTR’s Value 0046C060

    NOTE: This VPTR’s value is a address of Virtual table. Lets see in next Example.

    Example 7:

    OUTPUT:
    VPTR’s Address 0012FF7C
    VIRTUAL TABLE ‘s Address 0046C0EC
    Value at first entry of VIRTUAL TABLE 0040100A
    Test: fun1

    Example 8:

    OUTPUT:

    VPTR’s Address 0012FF7C
    VIRTUAL TABLE ‘s Address 0046C0EC
    Value at first entry of VIRTUAL TABLE 0040100A
    Value at 2nd entry of VIRTUAL TABLE 004012

    Example :9

    OUTPUT:

    Test::fun1
    Test::func1

    Example 10: multiple Inheritance

    OUTPUT:

    Derive’s Size = 12

    Example 11: Calling Virtual Functions in case of Multiple Inheritance

    OUTPUT:

    Base1::fun
    Base1::func
    Base2::fun
    Base2::func
    Base3::fun
    Base3::func
    Drive::Fn
    Drive::Fnc

    By
    Asadullah Ansari

    • Hi! It’s Very Good Fundamental Example.
      How To Use 3 tier Architecture in C#

      Sent Me At=savaliyabhavesh@rediff.com

    • Hello Asadullah Ansari,
      Could you please help me understand better what you have said in “Example 11: Calling Virtual Functions in case of Multiple Inheritance” in pictorial diagram, i mean what does it translate to when you say “((int*)&obj+1)”” or ((int*)&obj+2)” ??

      I could translate “((int*)&obj+0)” to *__vptr address of class Derive but what does “((int*)&obj+1)”” or ((int*)&obj+2)” mean ??

      Could please help here in understanding.

      Thanks in advance,
      -bsvac

  5. soalannya kenapa program di atas tidak dapat di jalan kan….harap balas….email..zakrie3@Yahoo.com

    • Declaring something virtual in C++ means that it can be overridden by a sub-class of the current base class. But logically we will never need to override the constructors in the derived class. So if you declare virtual constructors it’s of no use.

Leave a Reply