What is Virtual Function in c++ ? Give any six rules to write Virtual Functions.
When one class inherits its properties to other class and both derived class as well as base class has same function name, then it becomes difficult to access the right function name. In this case, the base version of the function is made virtual and we can access the right function for using its address.
Rules for Virtual Functions :
When virtual functions are created for implementing late binding, we should observe some basic rules that satisfy the compiler requirements:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object Pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class versions must be identical. If two functions with the same name have different prototypes, C++ considers the as overloaded functions, and the virtual function mechanism is ignored.