Controlling Access to Class Members
You can increase the integrity of software built with C++ by helping control access to class member data and functions. Class members can be declared as having private, protected, or public access, as shown in the following table:
|
Type of Access |
Meaning |
|---|---|
|
Class members declared as private can be used only by member functions and friends (classes or functions) of the class. |
|
|
Class members declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class. |
|
|
Class members declared as public can be used by any function. |
Access control helps prevent you from using objects in ways they were not intended to be used. This protection is lost when explicit type conversions (casts) are performed.
Note
|
|---|
|
Access control is equally applicable to all names: member functions, member data, nested classes, and enumerators. |
The default access to class members (members of a class type declared using the class keyword) is private; the default access to struct and union members is public. For either case, the current access level can be changed using the public, private, or protected keyword.
Note