13.4 VIRTUAL FUNCTIONS
13.4.1 Need for virtual functions
Logically, when a pointer is pointing to derived class object, method from the corresponding derived class should be executed. When the pointer in question is a base class pointer, this is not happening. Somehow, the language must have a provision to circumvent this difficulty. This calls for the concept of virtual functions.
If we declare such a function as virtual in the base class then the problem gets solved. It may be noted that keyword virtual can be applied only to a method from the class. (In other words global functions cannot be declared virtual.) Let us study the syntax and effect of a virtual declaration on a base class function.
13.4.2 Declaring function virtual
Let us redefine ...
Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.