This concept is used to achieve run time polymorphism. The base class pointer can point to the object of base class as well as to the object of derived class. But the derived class pointer can only point to the object of derived class and not to the object of base class. If the base class pointer is pointing to the object of base class then it will access the properties of the base class as it has no concern with the derived class. If it is pointing to the object of the derived class then it will access those properties of the derived class which it has inherited from the base class and not own properties of the derived class. This is shown in the following example:
For example (C++):
class base
{
public:
void show( )
{
cout<<"\nBase";
}
};
class derived : public base
{
public:
void show( )
{
cout<<"\nDerived";
}
};
void main( )
{
base *ptr; //Base class pointer ptr
base bobj;
derived dobj;
clrscr( );
ptr=&bobj; //ptr points to the object of base class
ptr->show( ); //Base class function show( ) will be called
ptr=&dobj; //ptr points to the object of derived class
ptr->show( ); //Base class function show( ) will be called
which is inherited by derived class and not
own show( ) function of the derived class
getch( );
}
Here ptr is the pointer of the base class. First of all it points to the object of the base class so it will access the show( ) function of the base class as it has no concern with the derived class. Then ptr points to the object of the derived class but still it will access the show( ) function of the base class which is inherited by the derived class and not the own function of the derived class. This happens because the complier sees only the type of the pointer and not the contents of the pointer that is to which object the pointer is currently pointing to. So irrespective of the contents of the pointer the compiler decides that the pointer will access only the base class properties as it is of type base class.
Now if we want that the base class pointer should access the show ( ) function of the derived class and not of the base class when ptr is pointing to the derived class object, we will have to make the base class show ( ) a virtual function by using the keyword ‘virtual’. It is shown below:
class base
{
public:
virtual void show( ) //show ( ) function is made virtual
{
cout<<"\nBase";
}
};
class derived : public base
{
public:
void show( )
{
cout<<"\nDerived";
}
};
void main( )
{
base *ptr; //Base class pointer ptr
base bobj;
derived dobj;
clrscr( );
ptr=&bobj; //ptr points to the object of base class
ptr->show( ); //Base class function show( ) will be called
ptr=&dobj; //ptr points to the object of derived class
ptr->show( ); //Derived class function show( ) will be
called which is own show( ) function of the
derived class
getch( );
}
Here ptr is the pointer of the base class. First of all it points to the object of the base class so it will access the show ( ) function of the base class as it has no concern with the derived class. Then ptr points to the object of the derived class and it will access the show( ) function of the derived class which is own function of the derived class. This happens because we have made the base class show ( ) function a virtual. So the complier sees contents of the pointer that is to which object the pointer is currently pointing to.
Second Page of Virtual Function >>