Udostępnij za pośrednictwem


Class Protocol Implementation

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 all. 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:

  • An appropriate Identify function must be implemented.

  • An appropriate 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:

// deriv_ClassProtocolImplementation.cpp
// compile with: /LD
class Document {
public:
    //  Requirements for derived classes: They must implement
    //   these functions.
    virtual char *Identify() = 0;
    virtual char *WhereIs() = 0;
};

See Also

Reference

Overview of Derived Classes