LeeLin blog
(1) When the base class does not have its own constructor, the default bar of the derived class uses the constructor of the base class.
(2) When the base class has a self-compiled constructor, you must add a constructor without parameters to the base class.
(3) When the Base class has a self-compiled constructor and no non-argument constructor is added to the Base class, the Base class constructor must be specified by the Base class in the derived class.
Note: The initialization sequence is from the base class to the derived class. During initialization, the base class constructor is called to initialize the base class, and then the constructor of the derived class is called to initialize the derived class.
The following describes the derivation and Inheritance of C ++ for your reference.
Derivation and inheritance
1. Concept of a derived class
From the perspective of object-oriented programming, inheritance expresses the relationship between object classes. This relationship allows an object to inherit from another object.
Features and capabilities. If a class object inherits the features and capabilities of another class object, this class is called the derived class of the inherited class.
1.1 The general format of declaring a derived class is:
Class derived class name: the derived method (public or private) base class name {
// Newly added or modified data and member functions of the derived class };
1.2 execution sequence of the constructor and destructor of the derived class
When the derived class does not contain object members
● When creating a derived class object, the execution sequence of the constructor is: base class constructor → derived class constructor;
● When revoking a derived class object, the execution sequence of the Destructor is: constructor of the derived class → constructor of the base class.
When a derived class contains object members
● When defining a derived class object, the execution sequence of the constructor is: base class constructor → object member constructor → derived class constructor;
● When revoking a derived class object, the execution sequence of the Destructor is: constructor of the derived class → constructor of the object member → constructor of the base class ..
1.3 constructor and destructor construction rules of derived classes
When the base class does not explicitly define a constructor or has a function but no parameter, the derived class can not pass parameters to the base class, or even do not define a constructor;
When the base class contains constructors and parameters, the derived class must define constructors to pass parameters to the base class constructors.
(1) The general format of the derived class constructor is:
Derived class: derived class constructor name (parameter table): base class constructor name (parameter table ){
// Add a member to a derived class
}
(2) When a derived class contains object members, the constructor generally takes the following form:
Derived class: derived class constructor name (parameter table): base class constructor name (parameter table), object member name (parameter table ),...... Object member name n (Parameter
Table)
{// New data initialization (excluding object members)
}
2. Multi-Inheritance
The derived classes we introduced earlier have only one base class, which is called single-base Derivation or single inheritance. In practice, we often need a derived class to have multiple base classes at the same time.
This method is called multi-base Derivation or multi-inheritance.
2.1 Statement of multiple inheritance:
In C ++, the form of a derived class with more than two base classes is similar to that of a single base class. You only need to separate multiple base classes to be inherited with commas.
In multi-inheritance, public and private derivation have the same accessibility for the base class members in the derived class as the rule for single inheritance.
In addition, access to base class members must be unambiguous. If two base classes have data members or member functions with the same name, use the member name limitation to eliminate ambiguity.
If the new member or member function in the derived class has the same name as the base class member or member function, the derived class will overwrite the member with the same name as the outer class, And the scope identifier must also be used.
2.2 multi-inheritance constructor and destructor:
The definitions of Multi-inheritance constructor are similar to those of single-inheritance constructor. Only constructors of n base classes are separated by commas.
The execution sequence of the Multi-inherited constructor is the same as that of the single-inherited constructor. The constructor of the base class is also followed before the object member is executed.
Constructor. Finally, execute the constructor principle of the derived class. Between multiple base classes, the sequence is arranged from left to right according to the declaration of the derived class. The destructor
The execution order of the number is opposite to that of the constructor.
2.3 virtual base class:
If some or all of the direct base classes of a derived class are derived from another common base class, among these base classes, the members inherited from the base class at the upper level will have
If the name is the same, when accessing members of the common base class in this derived class, it may produce ambiguity. In this case, you can define a virtual base class. This requires that
In the definition of the base class, use the keyword virtual to define the common base class as a virtual base class. The syntax format is as follows:
Class derived class name: base class in virtual Mode
The initialization of the virtual base class is syntactically the same as that of the general multi-inheritance class, but the calling sequence of the constructor is different. The Calling sequence of the virtual base class constructor is different.
This is the rule:
1) at the same level, the constructor of the virtual base class is called first, followed by the constructor of the non-virtual base class, the constructor of the object member, and the constructor of the derived class.
Number.
2) If the same level contains multiple virtual base classes, the constructor of these virtual base classes calls them in the order they are described
3) if the virtual base class is derived from a non-virtual base class, the base class constructor is still called before the derived class constructor is called.
3. Note
1. The base class provides its behavior and structure to the derived class. The derived class is responsible for correctly initializing the base class object.
2. Call the constructor of the base class with the correct parameters as part of the constructor of each derived class (discussed)
3. Common member functions cannot use this syntax to call basic class methods.
4. A class is only responsible for the construction of its direct base class. However, there are different virtual base classes.
5. The parameters of the derived class constructor should include the parameters you need to use and the basic class.
6. The derived class inherits the behavior and structure of the Base class, but does not inherit the constructor and destructor.
7. You need to call the copy constructor of the base class in the copy constructor of the derived class.
8. You need to call the assignment operator of the base class in the assignment operator of the derived class.
9. The derived class destructor do not explicitly call the basic class destructor.
10. The used virtual base class is initialized by the constructor of the final derived class. When creating an object, the sub-object constructor's
Call.
11. Public inheritance is the main mode of inheritance. Private inheritance is only used in special cases (for example, stack classes can be inherited from List classes, but it is not a list.
Exports the members of the private base class .) Private inheritance has no polymorphism.
12. Do not use multiple inheritance when a single inheritance can achieve the goal
13. inherited advantages: code reuse, adding new classes and new features (such as satellite and patient monitoring), dynamic Association, and Polymorphism to running programs.