Chapter 1 tools for large programs (13)

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.