17.3 multi-inheritance and virtual inheritance
Multiple inheritance can more directly model applications. Multi-inheritance is the ability to inherit from more than one direct base class derived class. Multi-inheritance Derived classes inherit the attributes of all their parent classes.
17.3.1 multi-Inheritance
1. Define multiple classes
The derived class specifies the access level-public, protected, or private for each base class (explicitly or implicitly. Like single inheritance, classes can be used as the basis classes for multi-inheritance only after being defined. There is no language restriction on the number of base classes that a class can inherit. However, in a given derived list, a base class can only appear once.
Class ZooAnimal {};
Class Endangered {};
Class Bear: public ZooAnimal {};
Class Panda: public Bear, public Endangered {};
Class ZooAnimal {};
Class Endangered {};
Class Bear: public ZooAnimal {};
Class Panda: public Bear, public Endangered {}; 2. Multi-inherited Derived classes inherit the State from each base class
Under Multi-inheritance, the object of the derived class contains the base class sub-objects of each base class.
3. The derived class constructor initializes all base classes.
Objects that construct a derived type include all base class sub-objects that construct and initialize it.
Namespace andsoft
{
Namespace Animal
{
Class ZooAnimal {};
Class Endangered {};
Class Bear: public ZooAnimal {};
Class Panda: public Bear, public Endangered {
Public:
Panda (): Bear (), Endangered (){}
};
}
}
Namespace andsoft
{
Namespace Animal
{
Class ZooAnimal {};
Class Endangered {};
Class Bear: public ZooAnimal {};
Class Panda: public Bear, public Endangered {
Public:
Panda (): Bear (), Endangered (){}
};
}
}
4. Structure order
The constructor can only control the value used to initialize the base class, but cannot control the construction order of the base class. The base class constructor is called according to the sequence in which the base class constructor appears in the class derivation list.
The call sequence of the constructor is not affected by the base classes that appear in the constructor initialization list, nor by the order in which the base classes appear in the constructor initialization list.
5. Structure order
The Destructor is always called in reverse order according to the constructor running.
From xufei96's column