In C ++, a C ++ class is a logical unit that provides encapsulation. Each object of a class contains a set of data describing its own State, this data set is also processed by receiving specific messages. If the program designer can crop the class by adding, modifying, or replacing part of the content of the specified class, it can adapt to different applications, in this way, the value of data encapsulation is greatly enhanced, and the inheritance discussed next can fully implement this operation.
Inheritance is a process. through inheritance, an object can obtain attributes of another object, including functions, and add its own features to it. As an important mechanism of the C ++ language, the inheritance method can automatically provide an operation and data structure from another class for one class, in this way, programmers can quickly create a new class on the basis of a general class without having to design each class from scratch.
When a class is inherited by other classes, the inherited class is called the base class, but not the chicken ribs ^_^), or the parent class. The C ++ class that inherits other class attributes is called a derived class and also a subclass.
Generally, the inherited process originates from the definition of a base class, which defines the public attributes of all its derived classes. Essentially, the base class has the public attributes in the same class set. The derived class inherits these attributes and adds its own unique attributes. The essence of inheriting from any existing class is to build a new derived class.
The inheritance derived from a base class is called single inheritance. In other words, a derived class has only one direct base class. The common format of a single inheritance declaration statement is:
- Class derived class name: Access Control keyword base class name
- {
- Data member and member function declaration
- };
In contrast, inheritance derived from multiple base classes is called multi-inheritance or multi-inheritance. That is, a derived class has multiple direct base classes. In some object-oriented languages such as Java, multi-inheritance between classes is not supported, but only single-re-inheritance is supported. That is, a class can only have one direct parent class at most, therefore, you need to use interfaces and other mechanisms to implement similar functions. The syntax support for multiple inheritance in C ++ makes the problem much simpler. The common syntax of multiple inheritance declaration statements is:
- Class derived class name: Access Control keyword base class name 1, access control keyword base class name 2 ,...
- {
- Data member and member function declaration
- };
In addition to multi-inheritance, a derived class inherits multiple base classes, and another method is to use the derived class as the base class for another class to inherit again, resulting in a multi-level inheritance relationship. For example, Class A is A Class B, Class B is A derived class C, Class A is the direct base class of Class B, and Class B is the direct base class of class C, class A is the indirect base class of class C.
The hierarchy of classes is also called the inheritance chain. In the above example, when a c ++ class object is created, the constructor of Class A is called first, and then the constructor of Class B is called, finally, it is the constructor of class C. The Calling sequence of the Destructor is the opposite. When a derived class inherits a hierarchical class, each derived class on the inheritance chain must pass the required variables to its base class.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design