Reputation: 43
Is it necessary to always provide your base class with a constructor? For example if I had the following base class, and then some derived classes.
class Animal
{
public:
//Is this constructor necessary?
Animal();
virtual ~Animal();
};
class Zebra: public Animal
{
public:
Zebra();
~Zebra();
};
class Elephant: public Animal
{
public:
Elephant();
~Elephant();
};
Assuming that I am creating Animal pointers and then dynamically allocating new Zebra and Elephant objects. If I never intend to create an Animal object, then is there any actual need to explicitly create an Animal constructor?
Upvotes: 3
Views: 44
Reputation: 218278
You don't need to explicitly provide constructor. A default one is generated (if you don't provide other constructor).
so
class Animal
{
public:
virtual ~Animal();
};
is sufficient.
Derived classes constructors would call Base class constructor (explicitly of implicitly).
Upvotes: 2