As a quick refresher, public members can be accessed by anybody. Private members can only be accessed by member functions of the same class or friends. This means derived classes can not access private members of the base class directly! This is pretty straightforward, and you should be quite used to it by now.
public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class.
When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.
Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.
The default is private inheritance. take this example:
class B { };
class D: B { };
uses private inheritance as its the default. This means that D gets all the protected and public fields and methods that B has (if we actually declared any), but can't be cast to a B. Therefore, this code fails:
void foo(B* argument) {}
foo(new D); //not allowed
If D publicly inherited from B, then a D could be cast to a B, and this function call would be fine.
The second difference is that all the protected and public members in B become private members in D.
What does this actually mean? Public inheritance means D IS_A B, but private inheritance means "is implemented in terms of". Inheriting D from B means you want to take advantage of some of the features in B, but not because D IS_A B or because there's any conceptual connection between B and D. :D
Without that 'public' 'Employee' would become a private base class of 'Manager'.
Classes declared with keyword 'class' have their members private by default, and have their base classes private by default.
Classes declared with keyword 'struct' have their members public by default, and have their base classes public by default.
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