C++ provides a solution to invoke the exact version of the member function, which has to be decided at runtime using virtual functions. They are the means by which functions of the base class can be overridden by the functions of the derived class.
When declaring the base class member functions, the keyword virtual is used with those functions, which are to be bound dynamically. The syntax of defining a virtual function in a class is
Virtual return_type Function_name(arguments)
{
………….
………….
}
Virtual functions should be defined in the public section of a class to realize its full potential benefits. When such a declaration is made, it allows to decide which function to be used at runtime, based on the type of object, pointed to by the base pointer, rather than the type of the pointer.
No comments:
Post a Comment