April 14, 2016

Pure Virtual Functions and Memory Issues ..Part II

Hello ..

Continuing from Part 1.

Let me give a brief information about pure virtual functions.

Virtual Function
Any function that is declared in the base class and later redefined in the derived class is called as a virtual function.


class Univeristy
{
   int a;
   public:
       StudentInfo()
       {
          a = 1;
       }
       virtual void Display()
       {
          cout <<a;
       }
};

class College: public Univeristy
{
   int b;
   public:
       TotalStudentInfo()
       {
          b = 2;
       }
       virtual void Display()
       {
          cout <<b;
       }
};

int main()
{
   University *pA;
   College oB;
   pA = &oB;
   pADisplay();
   return 0;
}
 


In the above example we see that the function Display() has been declared virtual and in the base class it is made to display the value of a. However, in the derived class we have redefined the function to display the value b.

We know that the virtual function is a type of dynamic polymorphism  where the function call is resolved at the run time (also known as the dynamic binding) unlike the non virtual functions that go through the static binding process.

Let me share my understanding of how the virtual functions go through the dynamic binding process.

C++ uses a form of late binding known as the virtual table. As the name suggests, virtual table is a form of lookup table . The contents of the lookup table are the virtual functions.
The other names of the virtual table can be “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Here are some key points on virtual table :

  • Every class that has a virtual function is given a virtual table
  • Also any class that derives another class having a virtual function has its own virtual table
  • The table can be visualized as an array that compiler sets up at the time of compilation.
  • The table contains entries of the virtual functions that can be called by the objects of the respective class
  • So, each entry in this table is basically a function pointer which points to the function that is accessible by the class.
In our example above
  • There are two virtual tables. i.e. one for the base class ( University ) and one for the derived class College.
  • The entry in each of the virtual table would be of the function Display()  as it was declared to be a virtual function.
  • The entry is the function pointer to Display which is accessible to the object of each class. 
  •  The function pointer of  class Univeristy  will point to the function Display()  and will print the value of  "a" which is 1.
  • The function pointer of the class College  will point to the function Display()  which is defined in its class and will print the value of "b"  which is 2.

I have tried making a very rough sketch of the above concept



 As we see that the Display() has separate pointer to the derived class and the base class.





Pure Virtual Function
 


Abstract Class
Any class that contains a pure virtual function is an Abstract Clss.

To be Continued...


No comments:

Post a Comment