At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.
A virtual function is a function, defined in a superclass, that must be present in a subclass for that subclass to have a complete class definition. Virtual functions rely on an object-oriented programming paradigm called virtual inheritance, which is most commonly seen in C++ using the "virtual" keyword. To define a virtual function, two classes are necessary, a superclass and a subclass. The superclass is where the function is first declared, and possibly defined. The subclass is where the function is defined — or overridden, depending on whether the function was defined in the superclass.
The virtual function can be defined in one of two ways. First, it can be defined as a stub, in which it has an empty body and does nothing. Second, it could be defined as a pure virtual function, where it is defined as NULL in the superclass's header file.
There are advantages and disadvantages to both methodologies. Defining a function as a stub ensures that all subclasses have some implementation of it, even if it does nothing. If one forgets to override the function and implement it properly in a subclass, however, no errors or warnings will appear to point this out. Defining a pure virtual function, on the other hand, requires each subclass to have its own definition of the function, and errors will appear if this isn't the case.
Virtual functions, however, are subject to the same inheritance rules as non-virtual functions, so inheritance hierarchies with more than two levels might not require explicit definitions of virtual functions. For example, one can consider a class A that declares a virtual function, which is implemented in subclass B. Class B has a subclass of its own, class C. Class C does not require an explicit definition of class A's function, because it inherits the definition from class B. If necessary, class C could override class B's function, or it could override class B's function while also calling it.
At the other extreme, virtual functions don't have to be defined in a subclass if they are declared virtual in that subclass. For example, one can consider a class A that declares a virtual function and has two subclasses, B and C. In addition, one could imagine that class B has subclasses D and E, and subclass C has subclasses F and G.
Classes B through G all must have class A's virtual function defined somehow. If class B has an implementation of A's function, classes D and E don't need it to be redone. Perhaps C's subclasses need to implement A's function, but they both do something different, so defining the function in class C itself wouldn't be useful. In that case, the function may be declared virtual in class C, and an implementation is not necessary.
Virtual functions can be daunting to learn, but when used properly, they can reduce code duplication and make code much easier to understand in general. There are many pitfalls with virtual functions, however, especially concerning multiple inheritance. In multiple inheritance, it is possible for ambiguously defined virtual functions to conflict with each other, so they should be used with caution in that context.