As a per a discussion mentioned in 0033, all data members of a class should be private. This prevents any direct access to data members in a class. If a consumer needs to change a data member of a class, it has to use some member functions. This provides one layer of indirection that permits very dramatic changes to the implementation without any changes to the interface.
Certain member functions should be private, as well. To determine whether a member function should be private, you need ask whether the function is abstract (and does not depend on the implementation). If a member function requires changes to its prototype if the implementation of the class changes, it should be private.
Because all private members of a class are only visible to member functions of the same class, private members cannot be a part of the user interface.
By default, all data and function members in class are private
.
You can also explicitly indicate a section of a class definition is
private by using the keyword private
, followed by a colon, before
the section that should be private.
Copyright © 2006-09-07 by Tak Auyeung