[class.abstract] (original) (raw)

11 Classes [class]

11.7 Derived classes [class.derived]

11.7.4 Abstract classes [class.abstract]

[Note 1:

The abstract class mechanism supports the notion of a general concept, such as a shape, of which only more concrete variants, such ascircle and square, can actually be used.

An abstract class can also be used to define an interface for which derived classes provide a variety of implementations.

— _end note_]

A virtual function is specified as a pure virtual function by using apure-specifier ([class.mem]) in the function declaration in the class definition.

[Note 2:

Such a function might be inherited: see below.

— _end note_]

A class is an abstract classif it has at least one pure virtual function.

[Note 3:

An abstract class can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it ([basic.def], [class.mem]).

— _end note_]

[Example 1: class point { };class shape { point center;public: point where() { return center; } void move(point p) { center=p; draw(); } virtual void rotate(int) = 0; virtual void draw() = 0; }; — _end example_]

[Note 4:

A function declaration cannot provide both a pure-specifierand a definition.

— _end note_]

[Example 2: struct C { virtual void f() = 0 { }; }; — _end example_]

[Note 5:

An abstract class type cannot be used as a parameter or return type of a function being defined ([dcl.fct]) or called ([expr.call]), except as specified in [dcl.type.simple].

However, pointers and references to abstract class types can appear in such contexts.

— _end note_]

A class is abstract if it has at least one pure virtual function for which the final overrider is pure virtual.

[Example 3: class ab_circle : public shape { int radius;public: void rotate(int) { } };

Since shape​::​draw() is a pure virtual functionab_circle​::​draw() is a pure virtual by default.

The alternative declaration,class circle : public shape { int radius;public: void rotate(int) { } void draw(); };would make class circle non-abstract and a definition ofcircle​::​draw() must be provided.

— _end example_]

[Note 6:

An abstract class can be derived from a class that is not abstract, and a pure virtual function can override a virtual function which is not pure.

— _end note_]

Member functions can be called from a constructor (or destructor) of an abstract class;the effect of making a virtual call ([class.virtual]) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.