Opentopia Directory Encyclopedia Tools

Virtual function

Encyclopedia : V : VI : VIR : Virtual function


A virtual function or virtual method is an important part of the polymorphism portion of object-oriented programming (OOP). A virtual function allows a derived class to override functions in classes it inherits from - even when the derived class is cast as the type of object it inherits from.

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.

The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

Example

For example, a base class Animal could have a virtual function eat. Subclass Fish would implement eat() differently than subclass Wolf, but you can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific subclass.

This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling eat()), with no knowledge of what kind of animal may be in the list. You also do not need to have knowledge of how each animal eats, or what the complete set of possible animal types might be.

The following is an example in C++:

#include 

class Animal };

class Wolf : public Animal };

class Fish : public Animal };

class OtherAnimal : public Animal ;

int main()

output:

I eat like a generic Animal.
I eat like a wolf!
I eat like a fish!
I eat like a generic Animal.

Abstract Classes and Pure Virtual Functions

A pure virtual function or pure virtual method is a virtual function that has a declaration (signature), but no definition (implementation). This is often used in a base class which is a conceptual abstraction of the classes that inherit from it such that it is nonsensical to define the particular function. Such classes are referred to as "abstract" and cannot be instantiated since they have an undefined function. An example might be a base class "MathSymbol" which has a virtual function doOperation(). You might then derive class "Plus" or "Minus" which would define doOperation(), but defining doOperation() wouldn't make sense in the "MathSymbol" class.

Pure virtual functions are also used where the method declarations are being used to define an interface for which derived classes will supply all implementations. An abstract class serving as an interface contains only pure virtual functions, and not any data members or ordinary methods. Use of purely abstract classes as interfaces works in C++ as it supports multiple inheritance. Because many OO languages do not support multiple inheritance they often provide a separate interface mechanism. This is seen in Java for example.

C++

In C++, virtual functions can be explicitly marked as having no implementation by using a special syntax (example: class B ), and are then called pure virtual functions. The function prototype then serves as a stub that provides only the signature of the eventual implementing method. The actual definition or definitions will be provided later as overloaded functions in derived classes. (The compiler knows which method implementation to call at runtime by creating a list of pointers to all the virtual functions, called the vtable or virtual table.)

See also

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: