Protection levels in classes
C++ allows three levels of protection of attributes in classes. These are public, protected and
private.
These levels restrict access to identifiers, they do not hide them. Name conflicts with protected
attributes occur as if they were not protected at all.
- Private
-
A private attribute can only be used by functions within that class, by member initialisers and by
friend functions of that class. It is not accessible to code outside the class, nor to code in a sub-
class function. This is the most restrictive level of access.
-
Protected
-
A protected attribute cannot be accessed from outside the class, only within functions of its class or
classes derived from it publicly or protectedly or within friend functions of these classes. It is kept
in the family. This is important when using inheritance to define layered software designs, since
making an atribute private can seal it above a layer, but protecting an attribute leaves it open to use
in a controlled way.
-
Public
-
Public attributes are not hidden at all and can be accessed in the same way as members of structs.
Next note in series
Back to index