Base class definition

Could this be the definition of an abstract base class: "contains only pure virtual methods and often serves as an interface specification for derived classes"

or can the abstract base class also contain other methods (also virtual)

+6
source share
1 answer

By definition, from the C ++ standard (Β§10.4, Abstract classes, focus):

An abstract class is a class that can only be used as the base class of some other class; no objects of an abstract class can be created except subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function. [Note: such a function may be inherited: see below. -end note]

class point { / ... / }; class shape { // abstract class point center; public: point where() { return center; } void move(point p) { center=p; draw(); } virtual void rotate(int) = 0; // pure virtual virtual void draw() = 0; // pure virtual }; 

In the example, shape contains two pure virtual methods (which makes it an abstract class), but also contains two non-virtual methods. Everything is good. Thus, your initial definition that an abstract class contains only pure virtual functions is too compressed. Just one of them is enough.

+4
source

Source: https://habr.com/ru/post/981823/


All Articles