VirtualFunctions C++
 

 

MENU 

1) Artificial Project 1 Uninformed and Informed Search Algorithms.

2) Artificial Intelligence Project on Alpha Beta Pruning.

3) Artificial Intelligence

4) Computer Security ( Bell LaPadula Model)

5) Explicit and Implicit DLL Linking

6) Mathematical Software

7) Shell Scripting with Linux/Unix 

8) Shell Scripting with Linux/Unix ( Part 2

9) CS 8803 AIA Paper 2Title: Sleepers and Workaholics: Caching Strategies in Mobile Environments.



Virtual Functions in C++ is a very important and critical component. It is one of the advanced topic that ties up the object oriented paradigm of C++. So what exactly are virtual functions and how are they useful? 

In one line, virtual functions bring the concept of polymorphism to live. It is only through virtual functions that C++ brings the power of polymorphism to the developer.  Lets start with a very simple example. 

Class Animal

{

  virtual void makeNoise();

};

void Animal::makeNoise()

{

 cout<<" I am Animal Class";

}

In the Animal class above, there is a makeNoise function defined with a simple cout statement in it. 

class Cat public Animal

{

void makeNoise();

};

void Cat::makeNosie()

{

cout << "meow";

}

void main()

{

  Animal *parent;

  Animal animal;

  Cat  kitten;

  parent=&animal;

  parent->makeNoise()   // " I am animal class " gets printed

  parent=&kitten;

  parent->makeNoise()  // "meow" gets printed

}

you see, what is happening in the above code? it is purely at run time the decision is made of which makeNoise() function to call. The pointer parent points to two different object to the base class object and later to the inherited object. The v-table constructed or the virtual table determines at run time which function to call. 

Lets make the above code more interesting so that we can see the idea of v-table and the this pointer in more detail. 

Class Animal

{

virtual void a()=0;  //pure virtual function

virtual void makeNoise();

void parentAnimal();

};

void Animal::parentAnimal()

{

this->makeNoise();

}

void Animal::makeNoise()

{

cout << "Animal Class";

}

Class Cat public Animal

{

void makeNoise();

};


void Cat::makeNoise()

{

 cout<<"this is cat class";

}


void main()

{

    Cat kitten;

     kitten.parentAnimal();

}

The question here is, which makeNoise() function gets called?

The answer is, the makeNoise() function of the class Cat. The reason is that when kitten.parentAnimal() gets executed, the this pointer in the function parentAnimal() is of type Animal but is a pointer, pointing to kitten which is of type Cat. Therefore the code this->makeNoise() calls the function makeNoise() of the class Cat. The other way to understand this, is to consider the v-table constructed for the class Cat. Every object constructed of type Cat points to the v-table of class Cat because Cat cotains atleast one virtual function. The v-table is used at run time in determining at run time which function to call. The only way to know what v-table to look at, is to know what the 'this' pointer is pointing at.