Contents Up << >>

What are the access rules with "private" and "protected" inheritance?

Take these classes as examples:

  	class B                    { /*...*/ };
	class D_priv : private   B { /*...*/ };
	class D_prot : protected B { /*...*/ };
	class D_publ : public    B { /*...*/ };
	class UserClass            { B b; /*...*/ };
None of the subclasses can access anything that is private in B. In D_priv, the public and protected parts of B are "private". In D_prot, the public and protected parts of B are "protected". In D_publ, the public parts of B are public and the protected parts of B are protected (D_publ is-a-kind-of-a B). Class "UserClass" can access only the public parts of B, which "seals off" UserClass from B.

To make a public member of B so it is public in D_priv or D_prot, state the name of the member with a "B::" prefix. E.g., to make member "B::f(int,float)" public in D_prot, you would say:

  	class D_prot : protected B {
	public:
	  B::f;    //note: not  "B::f(int,float)"
	};
  • Abstraction