2009 Jul 26
Operators | OOP | Syntax | Virtual Functions |
External Links |
A Virtual Function is a function that is declared within a base class and redefined by a derived class. The function declaration in the base class is preceded with the keyword virtual.
The base class defines the interface and the derived class implements it. This is an example of polymorphism (one interface, multiple methods). The flexibility of virtual functions aris when an object pointer is used for objects with virtual functions. A run time decision is made as to which function to call depending on what the object pointer is pointing to at the time of execution. An object reference (rather than a pointer) can also be used.
The virtual attribute is inherited, meaning a virtual function of a derived class can be overriden by a class derived from the derived class.In some cases the base class does not have enough information to fully define a virtual function (other than its interface). Thus in the base class there is no definition for the virtual function, in which case it is called a pure virtual function and must have a derived class definition.
2009