Virtual Base Class Hierarchies

 

Some class hierarchies are broad but have many things in common. The common code is implemented in a base class, whereas the specific code is in the derived classes.

It is important for the base classes to establish a protocol through which the derived classes can attain maximum functionality. These protocols are commonly implemented using virtual functions. Sometimes the base class provides a default implementation for such functions. In a class hierarchy such as the Document hierarchy in the figure Sample of Directed Acyclic Graph (see Single Inheritance), two useful functions are Identify and WhereIs.

When called, the Identify function returns a correct identification, appropriate to the kind of document: For a Book, a function call such as doc->Identify() must return the ISBN number; however, for a HelpFile, a product name and revision number are probably more appropriate. Similarly, WhereIs should return a row and shelf for a Book, but for a HelpFile it should return a disk location — perhaps a directory and filename.

It is important that all implementations of the Identify and WhereIs functions return the same kind of information. In this case, a character string is appropriate.

These functions are implemented as virtual functions and then invoked using a pointer to a base class. The binding to the actual code occurs at run time, selecting the correct Identify or WhereIs function.

Show: