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 privateprotectedpublic 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 ringthe one corresponding to private member variables and functionsis the most constrained. The outer ringthe one corresponding to public member variables and functionsis the least constrained, inasmuch as there is no protection whatever.