Classes can be implemented to enforce a protocol. These classes are called “abstract classes” because no object of the class type can be created. They exist solely for derivation.
Classes are abstract classes if they contain pure virtual functions or if they inherit pure virtual functions and do not provide an implementation for them. Pure virtual functions are virtual functions declared with the pure-specifier (= 0
), as follows:
virtual char *Identify() = 0;
The base class, Document
, might impose the following protocol on all derived classes:
Identify
function must be implemented.WhereIs
function must be implemented.
By specifying such a protocol when designing the Document
class, the class designer can be assured that no nonabstract class can be implemented without
Identify
and WhereIs
functions. The Document
class, therefore, contains these declarations:
class Document
{
public:
...
// Requirements for derived classes: They must implement
// these functions.
virtual char *Identify() = 0;
virtual char *WhereIs() = 0;
...
};