C ++ Learning Article 12th-class inheritance

Source: Internet
Author: User

1. Introduction to inheritance

In the previous article, compound classes have been introduced. Composite classes are only one of the main methods for creating complex classes in C ++.

This article introduces another method, namely class inheritance.

Composite classes create new objects by combining and connecting other objects; inherit, create new objects by directly obtaining the attributes and behaviors of other objects, and expand and specialize them at the same time.

For example, you inherit the genes of your parents, update your technology, and C ++ inherit many features of C and also add your own features.

Parent class (base class)-inherited object; subclass class-inherited object.

The inherited relationship is a is-a relationship, that is, the Child class can be processed as the parent class;

Generally, a subclass inherits all attributes of the parent class. A subclass can define or redefine inherited attributes, add new attributes, or even hide attributes.

Reasons for inheritance in C ++:

1) One of the most basic features of OOP is the reusability of Code. However, existing code cannot meet your needs. The common method is to modify existing code, but this is not the best method.

2) one of the better methods is to copy the existing code and then modify it. But this method has many disadvantages: a) it is very dangerous because the copy code may have errors or omissions. B) to modify existing code, you must fundamentally understand the existing code. If the existing code is complex, it is very difficult. c) after modifying and correcting the code, you must maintain the synchronization of the original code, A large amount of maintenance.

3) inheritance is an effective method for most problems. Inheritance allows you to use existing code to meet your requirements.

2. c ++ basic inheritance

It inherits from C ++ and usually appears between classes. When a class inherits other classes, the class inherits the variables and functions of the parent class. These variables and functions become part of the class.

One person class:


The person class contains the basic information of a person;

A baseballplayer class:


Baseballplayer also needs to contain information such as name, age, and gender. However, a person class has been defined just now, so baseballplayer can inherit the person class:


Now, test the role class baseballplayer:


One emploee plugin class:


Inheritance chain:


Through the inheritance chain, we can define a series of reusable code.

Summary: through inheritance, we can obtain the information of the parent class without redefinition. When the parent class changes, the Child class also changes accordingly;

3. Sequence Class Construction Sequence

In the previous section, the class can inherit variables and functions from other classes. This section mainly describes the construction sequence of the classes.

There are two new classes:


The drived class consists of two parts: one is the base class and the other is the callback class.

In instantiation:


Base is a non-struct class. You only need to execute your own default constructor. drived is the struct class that inherits the base. During the instantiation process, you need to execute the base and its own constructor;

During the construction process, the category class traverses the inheritance tree and constructs each inherited part.

The following shows the sequence of constructing the category class:


The printed result is:


As shown in the preceding figure, when constructing a category class, the base class is constructed first. After all, there is no parent class and no child class. The Child class uses the variables and functions in the parent class, but the parent class does not know the Child class, first, construct the base class to ensure that the subclass can use initialized variables.

2) build order in the inheritance chain


The printed result is:


As shown above, the construction order in the inheritance chain is the first construction of the base class at the highest level, and is gradually constructed along the inheritance tree.

4. constructor and initialization of the category class

This section describes in detail the constructor of the category class and the initialization rules.

In the previous section:


The instantiation of base is as follows:


Because base is a non-member class, you only need to consider initialization of your own members;

The construction process is:

A) allocate memory space for cbase; B) suitable base constructor calls; c) initialize the list initialization variable; d) execute the constructor; e) return to the caller.

The instantiation of drived, such:


The construction process is:

A) allocate memory space for cdrive, which is sufficient to carry the base part and drived part; B) Call the appropriate drived constructor; c) construct the Base Object first;

D) initialize the list initialization variable; e) execute the constructor; f) return to the caller.

The main difference between the primary class and the non-primary class is that the primary class must first construct the base class object.

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.