Coupon Accepted Successfully!


Virtual Functions

Virtual functions of the base class should be redefined in the derived classes. The programmer can define a virtual function in a base class, and can then use the same function name in any derived class, even if the number and type of arguments are matching. The matching function overrides the base class function of a similar name. Virtual functions can only be member functions. We can also declare the functions as given below:

int Base::get (int) and int Derived::get(int) even when they are not virtual.

The base class version is available to derived class objects via scope override. If they are virtual, only the function associated with the actual type of the object is available. With virtual functions, we cannot alter just the function type. It is illegal, therefore, to redefine a virtual function so that it varies only in the return type. If two functions with a similar name have different arguments, C++ compiler considers them different, and the virtual function mechanism is dropped.

Test Your Skills Now!
Take a Quiz now
Reviewer Name