VIRTUAL FUNCTIONS
Virtual functions are essential for particular polymorphism, one of the cornerstone of oop.
Virtual means existing in apperance but not in reality.
When virtual functions are used , a program that appears to be calling a function of one class may in reality be calling a function of different class.
Why are virtual functions needed?
Suppose you have a number of objects of different classes but you want to put them all in an array and perform a particular operation on them using the same function call.
For example , a graphic example or program includes several different shapes : a triangle, a ball , a square and so on.. .Each of these classes has a member function draw( ) that causes the object to be drawn on the screen.
So you want to draw the picture in a convinent way . One approach is to creat an array that holds pointers to all the different objects in the picture.The array may be defined as
shape *parr[100]; // array of 100 pointers to shapes
if you insert a pointers to all the shapes into this memory, you can draw an entire picture using a simple loop.
for(int i=0 ; i<N ; i++)
parr[i] -> draw ( );
This is a amazing capability. completely different functions are executed by the same function call. if the pointer in parr points to a ball , the function that draws a ball is executed. if it points to triangle then the function that draws a triangle is executed. This is called polymorphism which means differnt forms
So, by examples you can easily understand the virtual function topic.
What happens when a base class and derived classes all have functions with the same name , and you access these functions using pointers but without virtual functions.
This is without virtual keyword
#include <iostream>
using namespace std;
class base
{
public:
void show()
{
cout<<"base";
}
};
class derive1:public base
{
public:
void show()
{
cout<<"derive1 ";
}
};
class derive2:public base
{
void show()
{
cout<<"derive2 ";
}
};
void main ()
{
base* ptr;
derive1 d1;
derive2 d2;
ptr = &d1;
ptr->show();
ptr = &d2;
ptr->show();
}
Sample run: base base
As you see the function in the base class is always executed.The compiler ignores the contents of pointer and choose the member function that matches the type of the pointer.
........................................................................................................................................................................................
Lets make a single change in our program . We place the keyword virtual in front of the declaration for the show( ) function. So Now output of this program what would be ?
#include <iostream>
using namespace std;
class base
{
public:
virtual void show() // just a single change of word virtual
{
cout<<"base";
}
};
class derive1:public base
{
public:
void show()
{
cout<<"derive1 ";
}
};
class derive2:public base
{
void show()
{
cout<<"derive2 ";
}
};
void main ()
{
base* ptr;
derive1 d1;
derive2 d2;
ptr = &d1;
ptr->show();
ptr = &d2;
ptr->show();
}
Sample run . derive1 derive2
Now as you see the member functions of the derived classes , not the base class, are executed.