Using private, protected, public access modifiers when inheriting classes
Before studying this topic, it is recommended that you familiarize yourself with the following topic:
Contents
Search other websites:
1. Features of using access modifiers when inheriting classes
In C++, the access modifiers private, protected, public can be applied to base classes when they are inherited by derived classes. The action of one or another modifier affects the level of accessibility of elements of base classes and manifests itself in two cases:
- when classes form an inheritance hierarchy;
- when instances of inherited classes are created.
The general form of setting the level of access to the base class is as follows:
class DerivedClass : [access_modifier] BaseClass { // ... };
here
- BaseClass – the base class;
- DerivedClass – derived class;
- access_modifier – one of the access modifiers private, protected, public. The access modifier may not be present, in which case private access is accepted by default.
⇑
2. The private access modifier for the base class.
2.1. Restricting access for inherited classes
If class B is derived from base class A and base class A contains the private access modifier, then the following visibility rules apply:
- all private-members of class A are not available in class B;
- all protected elements of class A are visible in class B;
- all public-members of class A are visible in class B;
- all classes inherited from class B will not have access to the elements of class A (Figure 1).
Figure 1. The private access modifier for the class. Restricting access for class C in the class hierarchy
As you can see from Figure 1, from class B, you can access protected and public elements of class A. However, for class C, class A is completely private (restriction of the private modifier). If other classes (D, E, etc.) are inherited from class C, then class A for these classes will also be closed.
⇑
2.2. Access restrictions for instances (objects) of inherited classes
If classes A, B form an inheritance hierarchy and class A is the base for class B, then the following rules apply to instances of these classes:
- only public elements (data members, methods) are available from an instance of class A;
- from an instance of class B, any elements of class A are not available. Here the restriction of the private modifier before the name of class A is triggered (Figure 2).
Figure 2. The private access modifier for the class. No access to element of base class A from instance of derived class B
As you can see from Figure 2, any private members of base class A are private to instances of inherited class B.
⇑
3. The protected access modifier
The protected access modifier restricts access from instances of inherited classes and does not restrict access from methods of inherited classes. If two classes A, B form an inheritance hierarchy, and class A is declared base with the protected modifier, then the following rules apply:
- all protected and public elements of class A are accessible from methods of class B (Figure 3);
- all protected and public elements of class A are accessible from methods of the class inherited from class B (class C)
- there is no access to any element of class A from instances of derived classes (Figure 4).
Figure 3. The protected access modifier. Base class members declared as protected or public are accessible from methods of the inherited class
Figure 4. The protected access modifier for the class. No access to members of base class A from instances of any inherited classes (C, D, etc.)
As you can see in Figure 4, the members of class A are private to instances of derived classes B and C.
⇑
4. The access modifier public
If the public access modifier is used in the base class name, the following rules apply:
- protected and public members of the base class are accessible from methods of the inherited class;
- private members of the base class are not available from methods of the inherited class;
- public members of the base class are accessible from instances of the inherited class;
- private and protected members of the base class are not accessible from instances of the inherited class.
Figure 5 demonstrates the above rules.
Figure 5. The access modifier public for the inherited class
⇑
Related topics
⇑