case 1:
class ObjectCount {
private:
ObjectCount(){}
};
class Employee : private ObjectCount {};
case 2:
class ObjectCount {
public:
ObjectCount(){}
};
class Employee : private ObjectCount {};
In case1: ObjectCount constructor is private and inheritance is private . It gives compiler error
In case2: ObjectCount constructor is public and inheritance is private . this code is ok.
Can anyone explain how is it happening?
In first case, the Employee
C'tor cannot invoke its parent (ObjectCount
) C'tor, because it is private.
In the 2nd case, there is no problem for the Employee
C'tor to invoke the parent's ctor, since it is public.
Note that this is important since every class must use its parent constructor before activating its own.
The private inheritence means that other classes cannot use [or see] Employee
as a ObjectCount
, it doesn't change the visibility of ObjectCount
's c'tor, which must be accessable by the derived class in any case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With