Home Segments Index Top Previous Next

519: Mainline

To improve your understanding of the public, protected, and private parts of a class definition, contemplate the following diagram. Note that you can think of the private–protected–public distinction as a means for establishing three rings, each of which corresponds to a particular degree of member-variable and member-function protection.

*--------------------------------------------------------------* 
|  *-----------------------------------------------*           | 
|  |   *-----------------------------------------* |           | 
|  |   |  box   box's private member variables   | |           | 
|  |   |   ^    and member functions are         | |           | 
|  |   |   |    available only to member         | |           | 
|  |   |   |    functions defined in the         | |           | 
|  |   |   |    box class                        | |           | 
|  |   *-----------------------------------------* |           | 
|  |       |                                       |           | 
|  |       *-- box_car                             |           | 
|  |                                               |           | 
|  | box's protected member variables              |           | 
|  | and member functions are available            |           | 
|  | only to member functions defined              |           | 
|  | in the box_car and box classes                |           | 
|  *-----------------------------------------------*           | 
|                                                              | 
|  box's public member variables and member functions          | 
|  are available to ordinary and member functions everywhere   | 
*--------------------------------------------------------------* 

The inner ring—the one corresponding to private member variables and functions—is the most constrained. The outer ring—the one corresponding to public member variables and functions—is the least constrained, inasmuch as there is no protection whatever.