Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract base class definition

Tags:

c++

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)

like image 525
Kaushik Reddy Avatar asked Jan 31 '15 22:01

Kaushik Reddy


Video Answer


1 Answers

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.

like image 83
Barry Avatar answered Oct 05 '22 23:10

Barry