The C ++ language has a very important feature: Private derivation ensures that the methods in the C ++ base class can only be used indirectly by the methods of the objects in the derived class, but not externally, unless these methods have been redefined, the following is a detailed description.
Inheritance is a process. through inheritance, an object can obtain attributes of another object, including functions, and add its own features to it. As an important mechanism of the C ++ language, the inheritance method can automatically provide an operation and data structure from another class for one class.
- Summarize stack objects referenced in C ++/CLI
- Interpretation of the C ++ compiler compilation function
- C ++ syntax in C ++
- Several minutes to learn how to use C ++
- Examples of C ++ Operators
In this way, programmers can quickly create a new class on the basis of a general class without having to design each class from scratch. When a class is inherited by another class, the inherited class is called a base class or a parent class. A class that inherits other class attributes is called a derived class and also a subclass.
Generally, the inherited process originates from the definition of a C ++ base class. The base class defines the public attributes of all its derived classes. Essentially, the base class has the public attributes in the same class set. The derived class inherits these attributes and adds its own unique attributes. The essence of inheriting from any existing class is to build a new derived class.
The inheritance derived from a base class is called single inheritance. In other words, a derived class has only one direct C ++ base class. The common format of a single inheritance declaration statement is:
- Class derived class name: Access Control keyword base class name
- {
- Data member and member function declaration
- };
In contrast, inheritance derived from multiple base classes is called multi-inheritance or multi-inheritance. That is, a derived class has multiple direct base classes. In some object-oriented languages such as Java, multi-inheritance between classes is not supported, but only single-re-inheritance is supported. That is, a class can only have one direct parent class at most, therefore, you need to use interfaces and other mechanisms to implement similar functions. The syntax support for multiple inheritance in C ++ makes the problem much simpler. The common syntax of multiple inheritance declaration statements is:
- Class derived class name: Access Control keyword base class name 1, access control keyword base class name 2 ,...
- {
- Data member and member function declaration
- };
In addition to multi-inheritance, a derived class inherits multiple base classes, and another method is to use the derived class as the base class for another class to inherit again, resulting in a multi-level inheritance relationship. For example, Class A is A Class B, Class B is A derived class C, Class A is the direct base class of Class B, and Class B is the direct base class of class C, class A is the indirect base class of class C.
The hierarchy of classes is also called the inheritance chain. In the above example, when A class C object is created, the constructor of Class A is called first, and then the constructor of Class B is called, finally, it is the constructor of class C. The Calling sequence of the Destructor is the opposite. When a derived class inherits a hierarchical class, each derived class on the inheritance chain must pass the required variables to its base class.
In an inherited declaration statement, the access control keyword is used to indicate the extent to which the members and member functions declared in the base class definition can be accessed by the derived class. The access control keyword can be public, private, or protected. If the access control keyword is public.
The derived class is derived from the C ++ base class public. If the access control keyword is private, the derived class inherits from the C ++ base class private, also known as private. The differences between public inheritance and private inheritance are listed below.
Through the above table, we can summarize the features of the two types of derivation as follows:
| Base class member |
Base class private member |
Public Member of the base class |
Derivation Method |
Private |
Public |
Private |
Public |
| Derived class member |
Invisible |
Invisible |
Visible |
Visible |
| External Functions |
Invisible |
Invisible |
Invisible |
Visible |