Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Derivation and Access Control

 

Members of a class can be private, public or protected. As explained in Section gif, private members are accessible only by member functions and friends of the class in which the member is declared. In particular, this means that the member functions of a derived class cannot access the private members of the base classes even though the derived class has inherited those members! On the other hand, if we make the members of the base classes public, then all classes can access those members directly, not just derived classes.

C++ provides a third category of access control--protected. Protected members can be used by member functions and friends of the class in which the member is declared as well as by member functions and friends of all the classes derived from the class in which the member is declared.

A derived class inherits all the members of its base class(es). The accessibility of the members inherited by a derived class depends on whether the derivation is private or public:

class B { ... };
class D1 : public B { ... };
class D2 : private B { ... };
Here B is a public base class of D1. This means that the public members of B are also public in D1 and that the protected members of B are also protected in D1. On the other hand, B is a private base class of D2. This means that the public and protected members of B are private in D2.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.