Inheritance and derivation in C++

Inheritance in C++ is the relationship between classes. It is a very simple and intuitive concept. It is similar to inheritance in the real world. For example, a son inherits his father's property.

Inheritance (Inheri tan ce) can be understood as a process in which a class obtains member variables and member functions from another class. For example, class B inherits from class A, then B has member variables and member functions of A.

In C++, derivation (Derive) and inheritance are the same concept, but the point of view is different. Inheritance is when the son receives the father's inheritance, and derivation is when the father passes on the inheritance to the son.

The inherited class is called parent class or base class, and the inherited class is called subclass or derived class. "Subclass" and "parent class" are usually called together, and "base class" and "derived class" are usually called together.

In addition to the members of the base class, the derived class can also define its own new members to enhance the functions of the class.

Here are two typical scenarios for using inheritance:

1) When the new class you create is similar to the existing class, except for several member variables or member functions, you can use inheritance, which will not only reduce the amount of code, but also the new class will have all the functions of the base class.

Guess you like

Origin blog.csdn.net/shiwei0813/article/details/132590760