C ++: "inheritance and Derivation"
Concepts of inheritance and DerivationI. Definition1. base class (parent class): original class;
2. derived class (subclass): new class;
3. Inheritance: The derived class has the features of the base class;
4. Derivation: the process of generating classes from the base class. Similar derivation is called generalization in UML.
Ii. Syntax1. Definition of a derived class:
Class derived class name: The base class name of the Inheritance Method ..
{
...
};
Iii. Inheritance and combination1. Contact: when multiple inheritance occurs, a derived class has multiple direct base classes. The derived class is actually a combination of all base class attributes and behaviors. A derived class is an extension of the base class. Some members of the derived class come from the base class, so the derived class combines the base class. 2. Differences:
Inheritance: 1. describes the relationship between general classes and special classes;
2. "is a kind of"; combination: 1. Relationship between the whole and part of the description;
2. "is a part ";
Inheritance MethodThe inheritance methods include mutual inheritance, private inheritance, and protection inheritance.
Type compatibilityI. Features of Type compatibility1. A derived class object can be copied to a base class object;
2. The object of the derived class can initialize the reference of the base class;
3. The address of the derived class object can be assigned to the pointer to the base class;
Ii. AdvantagesIt can easily realize type conversion between the base class and the derived class, greatly reducing the burden of programming code and improving the low rate of programming.
Virtual base classI. Definition1. Format: class derived class name: virtual Inheritance Method Base class Name
2. virtual is a keyword used to declare the base class as the virtual base class of the derived class;
3. The scope of the keyword only applies to the base class that follows it;