// Whether a super-class member is accessible inside child-class or not is solely // determined by super-class access level. Therefore, the child-class of any // inheritance mode can access pub and prot.
// In contrast, the inheritance mode demotes super-class members: the effective // access is the stricter of the member's level and the mode's level // (public > protected > private), applied only to outsiders (external code and // further derived classes). // // Base member level | PUBLIC inherit | PROTECTED inherit | PRIVATE inherit // ------------------+----------------+-------------------+---------------- // public public protected private // protected protected protected private // private private private private // // 1) PUBLIC: access levels pass through unchanged. classPub : public Base { public: voidinside(){ std::cout << pub << prot; // OK: pub public, prot protected // priv; // ERROR: private in Base } };
// 2) PROTECTED: Base's public members demoted to protected. classProt : protected Base { public: voidinside(){ std::cout << pub << prot; // OK: both protected now } };
// 3) PRIVATE: everything collapses to private in Priv. classPriv : private Base { public: voidinside(){ std::cout << pub << prot; // OK: private in Priv, visible here } };