A class with one (or more) virtual pure functions is abstract, and it can't be used to create a new object, so it doesn't have a constructor.
I'm reading a book that provides the following example:
class Employee {
public:
Employee(const char*, const char*);
~Employee();
const char* getFirstName() const;
const char* getLastName() const;
virtual double earnings() const=0 // pure virtual => abstract class
virtual void print() const
private:
char* firstName, lastName;
};
If the class is abstract why we have a constructor? It uses this class later (Boss
is public derived from Employee
):
void Boss::Boss (const char* first, const char* last, double s)
: Employee (first, last)
To initialize firstName and lastName. Otherwise you will have to write a code to initilze them in each derived classes' constructors
"An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration."
regarding:
first
andlast
are defined in the base class, therefore, in order to initialize them, we need to make a call to the constructor of the base class: Employee (first, last)