Sunday, August 9, 2009

VIRTUAL FUNCTIONS

As mentioned earlier polymorphism refers to the property by which objects beloging to different classes are able to respond to the same message.But in different forms .Anessential requirements of polymorphism is therefore the ability to refer to objects without regard to their classses.This necessitates the use of single pointer variable to refer to the objects of different classes.here we use the pointer to base cllass to refer to all derived objects.But we just discovered that a base pointer,even when it is made to contain the address of derived class always execute the function in the base class.
The compailer simply ignores the contents of the pointer and chooses the member function that matches the type of the pointer .How do we then archieve polymorphysm.It aheived using what is known as virtual functions.
Whenwe use the same function inboth the base and derived classes,the function in base class is declared as virtual using the keyword"VIRTUAL". More

No comments:

Post a Comment