Inheritance can be defined by the new class contains all the properties of existing classes, so that many new functions can be implemented, practical application is also more convenient.
A class that already exists is called a base class or parent class , and the newly created class is called a subclass or derived class . Some concepts: single-inheritance, multiple-inheritance (multiple inheritance). Direct inheritance, indirect inheritance. Inheritance is transitive and asymmetric .
I. Defining methods and the composition of derived classes
Single inheritance: class class Name: Inheritance mode base class Name { ...}; Multiple inheritance: class name: Inherit Way base class name 1, Inherit way base class Name 2 ... { ......};
The way of inheritance is public, private and protected.
The three-step walk is composed:
① accepts base class members. In addition to constructors and destructors, the whole is accepted. So design carefully to avoid wasting space.
② to transform the base class members. Create a function that is identical to the name, type, parameter table of the member function in the base class, and so on.
③ add a new member. Add your own members.
Second, access control in the inheritance
1. Public inheritance
The derived class that this inheritance establishes is called a common derived class, and the base class is called the common base class.
As for access, I think it's good to use this chart and form in the book.
In derived classes, private (even derived classes) are not accessible, public or public, protected, or protected.
Where protect members are allowed only base classes and derived classes, that is, they can be used for their families, but private can only be used by themselves, and derived classes cannot be used.
2. Private inheritance
corresponding, private derived class, private base class.
3. Protection of inherited protected
Corresponding, protect the derived class, and protect the base class.
The main embodiment is on the indirect derived class. An indirect derived class can access the protected member at its upper level, so protected inherited by the base class member can also be used.
"C + +" C + + self-learning Advanced (5): Inheritance (1)--Getting Started