Ques : Rules for Virtual Functions

Ans :

 The virtual functions must be the members of some class.

 A class member function can be declared to be virtual by just specifying the keyword „virtual‟ in front of the function declaration. The syntax of declaring a virtual function is as follows:

virtual <retuen type><function name><(argument list)>
{//Function Body}

 Virtual Functions enables derived (sub) class to provide its own implementation for the function already defined in its base (super) class.

 Virtual Functions give power to the derived class functions to override the function in its base class with the same name and signature.

 Virtual Functions can‟t be static members.

 Only the functions that are members of some class can be declared as virtual that means we can‟t declare regular functions or friend functions as virtual.

 A virtual function can be a friend of another class.

 A virtual function in a base class must be defined, even though it may not be used.

 If one will call the virtual function with the pointer having the reference to the base class object then the function of the base class will be called for sure.

 The corresponding functions in the derived class must agree with the virtual function’s name and signature that means both must have same name and signature.

Leave a Reply