C++. Access modifiers: private, protected, public. Encapsulating data in a class

C++. Access modifiers: private, protected, public. Encapsulating data in a class


Contents





Search other websites:

1. Cases of using private, protected, public access modifiers

The object-oriented programming paradigm provides for the ability to set the level of accessibility of class elements. The accessibility level allows you to regulate access to data and methods of a class. Some elements of the class can be public, others are hidden. Hiding data and methods in a class is called encapsulation.

In C++, the accessibility level is defined using special modifiers private, protected, public. The use of these modifiers can be used in two ways:

  • in the case of declaring class members. This case is covered in this topic;
  • in the case of class inheritance. More details about the use of access modifiers in class inheritance can be found here.

 

2. Applying access modifiers to a class element
2.1. The access modifier private. Features of use

If an element (function, data member) is declared in a class in the private section, then the following access rules apply to it:

  • the element is inaccessible to any instances of methods of other classes or methods that are not “friendly” to the class (Figure 1);
  • the element is inaccessible from inherited classes (Figure 2);
  • the element is accessible from methods that are implemented in the class (Figure 3);
  • the element is accessible from friendly functions (Figure 4);
  • the element is accessible from methods of friendly classes (Figure 5).

 

C++. The private access modifier. No access from the class instance to the private-element of the class

Figure 1. The private access modifier. No access from the class instance to the private-element value of the class

C++. The private access modifier. There is no access to the private-member of the class from the inherited class

Figure 2. The private access modifier. There is no access to the private-member of the class from the inherited class

C++. The private access modifier. Access to the elements of the class from the internal class method

Figure 3. The private access modifier. Access to the elements of the class from the internal class method

C++. The private access modifier. Access to a class member from a friendly function (method)

Figure 4. The private access modifier. Access to a class member from a friendly function (method)

If in the code shown in Figure 4, in the declaration of the class A, before the name of the SetValue() function, the keyword friend is removed, then the access to the variable value of the class will not be available. As a result, in the SetValue() function in the line

objA.value = 33;

the compiler will throw an error like

Member A::value is inaccessible

 

C++. The private access modifier. Access to a private member of a class from a method of a friendly class

Figure 5. The private access modifier. Access to a private member of a class from a method of a friendly class

 

2.2. Access modifier public. Features of use

If an element with the public access modifier is defined in a class, then the rule is true:

  • the element is available to all methods in the program.

An exception is the case when the class is inherited as private. Then even public members of this class will not be available in inherited classes. You can read more about using class access modifiers here.

C++. The public access modifier. Access to the elements of the class from any method in the program

Figure 6. The public access modifier. Access to the elements of the class from any method in the program

C++. The public access modifier. Access from an inherited class

Figure 7. The public access modifier. Access from an inherited class

 

2.3. The protected access modifier. Features of use

The protected access modifier is relevant in cases where classes form an inheritance hierarchy. If an element (function, data member) with the protected access modifier is declared in a class, then the following rules apply to it:

  • a class element is not accessible from any external method (Figure 8) if this method is not friendly;
  • the class element is accessible from the internal methods of the class (Figure 8). It should be noted here that a protected-element of a class is also accessible from an instance of a class, if this instance is declared in an internal method of the class;
  • the element of the class is accessible from the friendly functions of the class (Figure 9);
  • a class element is accessible from methods of a friendly class (Figure 9);
  • the class element is accessible from the methods of the inherited class (Figure 10);
  • the class element is inaccessible from instances of the inherited class (Figure 11). This rule does not apply to “friendly” methods and methods of “friendly” classes.

 

Figure 8. The protected access modifier. Access is denied from a class instance if this instance is created in an “unfriendly” method

Figure 8 demonstrates two types of access to a protected class element:

  • through an instance (object) of the class, which is declared in the internal Method() method of the class;
  • direct access as a member of the class data.

 

C++. The protected access modifier. Access from "friendly" methods and methods of "friendly" classes

Figure 9. The protected access modifier. Access from “friendly” methods and methods of “friendly” classes

 

C++. The protected access modifier. Access from methods of an inherited class

Figure 10. The protected access modifier. Access from methods of an inherited class

 

C++. The protected access modifier. No access from inherited class instances

Figure 11. The protected access modifier. No access from inherited class instances

 


Related topics