C++. Inheritance. General concepts. Using private, protected, public modifiers

Inheritance. General concepts. Using private, protected, public modifiers when inheriting


Contents


Search other resources:

1. Reusing code. Inheritance

The idea of inheritance in programming is taken from nature and dates back to the 60s of the 20th century. C++ greatly enhances the concept of classes by introducing inheritance into classes. In nature, inheritance allows you to add new skills to parental qualities. In programming, inheritance is the property of a class to receive the program code (skills) of another (base) class, adding its own code to it, thereby expanding its capabilities.

Using the inheritance mechanism, you can change any class developed by the programmer himself or by other programmers without restrictions. In this case, you should not rebuild the structure of this class. New features are added to the base class. The capabilities of a base class in an inherited class can be extended, modified, narrowed, destroyed, or left unchanged.

Inheritance is only for classes and their characteristics, not for variables or functions.

Projects in the C++ language are based on the use of specific classes that solve assigned tasks. Through the use of inheritance, classes are built incrementally, from basic simple classes to specialized classes that progressively refine the solution. As a result, classes form a hierarchy. In these hierarchies, the top-level classes (base classes) describe some characteristics that are common to all, which, at lower levels, are detailed in the inherited classes. A completed software project is a workable system consisting of hierarchically interconnected classes. The number of classes in some projects can be tens or hundreds.

Unlike some programming languages, C++ allows multiple inheritance. With multiple inheritance, one class can be inherited from several base classes, getting their properties and behavior.

In the context of the concept of inheritance, the concept of code reuse is defined, which in C++ programming is defined by two aspects:

  • creation of a class for the purpose of receiving instances;
  • creation of a class to be used as a base class, passing its characteristics to inherited classes.

Deriving code from a base class is an efficient way to use already written code for your own needs. Inheritance is one part of code reuse.

 

2. The syntax for declaring classes that form a hierarchy. Base class and derived (inherited) class

To implement inheritance, at least two classes are required. If in a program a class named B is to be inherited from a class named A, then the class declaration looks like this:

class A
{
  // Elements of class A
  // ...
};

class B : A
{
  // Constituent elements of class B
  // ...
};

In the above declaration, class B inherits some or all of the characteristics of class A. From class B, another (third) class can be inherited, which will receive some or all of the characteristics of classes A and B. In addition, one or more classes can be inherited from class A, which, in relation to class B, form a parallel branch of the hierarchy. Figure 1 shows one of the possible options for the formation of a tree-like inheritance hierarchy by classes.

C++. Inheritance. Inheritance tree created by classes

Figure 1. Inheritance. Inheritance tree created by classes

 

3. Passing characteristics to an inherited class. Implementation options. Restriction and expansion of access. Keywords private, protected, public

An important question in inheritance is: how to transfer the characteristics of the base class to the inherited class? Here C++ gives a wide range of possible implementations.

Encapsulation is used to specify which members of a base class should be available in an inherited class. The basis of this mechanism is the use of the keywords private, protected, public to grant access or deny access to the elements of the base class.

3.1. The interaction of two classes. Access from an inherited class to elements of the base class

A base class element (variable, function) can be declared with one of three access modifiers:

  • private (hidden). In this case, there is no access to this element from the inherited class;
  • protected – allows you to use a base class element in an inherited class;
  • public – in the inherited class, acts in the same way as protected.

Figure 2 shows all 3 types of access for two classes that form an inheritance hierarchy. Demonstrates access to the variable a of the base class A from the Func() function of the inherited class B. The same rules apply not only for variables, but also for functions of the base class A.

C++. Inheritance for two classes A and B

Figure 2. Inheritance for two classes A and B. Private members of the base class cannot be accessed from the inherited class. Access to protected and public members of the base class is allowed

 

3.2. The interaction of two classes. Access from an instance (object) of an inherited class to elements of the base class using the private and protected access modifiers

If we consider access from an instance (object) of an inherited class to elements of the base class, then the difference from the previous case (clause 3.1) lies in the use of the protected keyword and the way the class is inherited. A class can be inherited as private, protected or public. If the class is inherited as private, then the private keyword is optional (see Figure 2).

Figure 3 shows the possible options for accessing the elements of the base class from an instance of the inherited class. In any case of inheritance of the base class, access to the elements of this class from an instance of the derived class is prohibited.

C++. Access modifiers private, protected prohibit access to class members

Figure 3. Access modifiers private, protected prohibit access to class members from any instance of the class

 

3.3. The interaction of two classes. Accessing members of a base class from a derived class. The public access modifier

If in the base class A a certain element is declared with the public access modifier, then an instance of the inherited class B:

  • has access to elements of the base class A if the derived class B inherits class A with an access modifier;
  • does not have access to the elements of the base class A if the derived class B inherits the class A as private– or protected-.

Figure 4 clearly shows all possible options for such access.

C++. Inheritance. Access to public elements of the base class

Figure 4. Access to public elements of the base class: 1) the base class is inherited as a private class; 2) the base class is inherited as a protected class; 3) the base class is inherited as a public class.

 

4. Inheritance of 3 or more classes. Access from inherited classes

As shown above, a base class can be inherited by prefixing its name with the modifiers private, protected, public. The difference in the use of these modifiers is in the class that is derived from the inherited class. That is, if three classes are given with the names A, B, C, successively inheriting each other, then the effect of modifiers will be exactly on class C.

 

4.1. Accessing the private base class from derived classes

Figure 5 shows access to base class A from derived class C. Class A in class B is inherited as a private class (hidden). This means that access to any element of class A from a derived class C is prohibited.

C++. Inheritance. Access to members of private class A from inherited class CFigure 5. Access to members of private class A from inherited class C is denied

 

4.2. Accessing protected- and public- base class from derived classes

If the base class A is inherited with the protected or public modifiers, then the elements of this class are available in the inherited classes B and C.

C++. Inheritance. The protected and public modifiers before the base class nameFigure 6. The protected and public modifiers before the base class name. Opening access to protected- and public-elements of the base class

 


Related topics