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

Member Access Control

 

Every member of a class, be it a variable or a function, has an access control attribute which affects the manner in which that member can be accessed. The members of a class can be private, public or protected.

By default, the members of a class are private. For example, the member variables real and imag declared in Program gif are both private. Private members can be used only by member functions and friends of the class in which the member is declared.

On the other hand, public members of a class can be used by any function. In Program gif the keyword public on line 5 indicates that all the following member functions are publicly accessible.

In effect, the public part of a class defines the interface to that class and the private part of the class encapsulates the implementation of that class. By making the implementation of a class private, we ensure that the code which uses the class depends only on the interface and not on the implementation of the class. Furthermore, we can modify the implementation of the class without affecting the code of the user of that class.

Protected members are similar to private members. I.e., they can be used by member functions and friends of the class in which the member is declared. In addition, protected members can also be used by member functions and friends of all the classes derived from the class in which the member is declared. The protected category is discussed again in Section gif.


next up previous contents index

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