tags:

views:

70

answers:

3

In C++, in what case, the derived class must have its own constructor?

what about the three cases: 1) public inheritance; 2) private inheritance; 3) protected inheritance;

Thanks a lot.

+6  A: 

All classes that are instantiated always have to have at least one constructor. If you don't provide one, the compiler will provide one instead.

There aren't any special rules for derived classes.

sbi
what about the case in which there is no default constructor in the base class?
skydoor
Even then, if an object of the class needs to be created, the class needs a suitable constructor. That can be a user-defined constructor, or one provided by the compiler. (For the nitpickers: the latter could be a compiler-defined cctor used to initialize the object by slicing a derived class' object.)
sbi
A: 

Constructors are not inherited, you have to write them explicitely for the derived class. Destructors are generated automatically, as a default constructor is generated, if the base class has a default constructor.

tobing
Compiler generated constructors will call base class constructors with no problems.
Martin York
Only if the base class has a default constructor.
tobing
A: 

Constructors are not inherited. Each class needs its own constructors.

UncleBens
But even if not defined each class will have its own constructor as the compiler will auto generate the default/copy constructors.
Martin York