Wednesday, January 1, 2014

Virtual Function

When you want to allow a derived class to override a method of the base class, that method should be defined as virtual method and with override keyword in derived class.

When a method declared as virtual in base class, then that method can be defined in derived class and it is optional for the derived class to override that method.

When it needs same definition as base class, then no need to override the method and if it needs different definition than provided by base class then it must override the method.

Method overriding also provides more than one form for a method. Hence it is also an example for polymorphism.

The virtual modifier tells the compiler that when any class derived from class A is used, an override method should be called.

No comments: