Search code examples
c++classabstractpure-virtual

Can a class still be pure abstract if it has a non-pure destructor?


I am working on an exercise which asks me to take a base class Rodent and make it a pure abstract class. My understanding of a pure abstract class is that it acts as an interface and only contains pure virtual functions. Although this is an easy exercise I have a problem with the solution provided by the book:

class Rodent
{
    public:

    virtual ~Rodent() {cout << "Destroy rodent" << endl;}
    virtual void run() = 0;
    virtual void squeak() = 0;
};

As you can see the author has added a dummy definition for the destructor. Does the adding of this definition not mean that this is an abstract class and not a 'pure' abstract class?


Solution

  • An Abstract class must contain atleast one pure virtual function.

    Your class already has two pure virtual functions run() and squeak(), So your class is Abstract because of these two pure virtual functions.

    You cannot create any objects of this class.

    EDIT:

    A pure abstract class, is a class that exclusively has pure virtual functions (and no data). Since your destructor is not pure virtual your class is not Pure Abstract Class.