Reputation: 85
Can this be a 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)
Upvotes: 6
Views: 666
Reputation: 304142
By definition from the C++ standard (§10.4, Abstract Classes, emphasis mine):
An abstract class is a class that 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. A class is abstract if it has at least one pure virtual function. [ Note: Such a function might 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. That is OK. So your original definition that an abstract class contains only pure virtual functions is too constricting. Just having at least one such is sufficient.
Upvotes: 5