1. Three types of class members
Public members can be accessed by any class,
Protected members are limited to themselves and subclass access. Used in the same class, whose access rights are equivalent to a private member, but when derived, his access rights are equivalent to that of a public member
Private members are limited to their own access, that is, they can only be used within their own classes.
2, for public inheritance mode:
• The visibility of the base class member to the derived class for the derived class,
The public and protected members of the base class are visible: Both the public members of the base class and the protected members as members of the derived class remain in their original state;
Private members of the base class are not visible: private members of the base class are still private, and derived classes cannot access private members in the base class.
Therefore, for a derived class object, the public members of the base class are visible and the other members are not visible. In public inheritance, the objects of the derived class can access public members in the base class, and the member functions of the derived class can access public and protected members in the base class. 3, for private inheritance way:
• The visibility of the base class member to the derived class for the derived class,
The public and protected members of the base class are visible: the public and protected members of the base class are both private members of the derived class and cannot be accessed by subclasses of this derived class;
The private members of the base class are not visible: derived classes cannot access private members in the base class.
Therefore, for a derived class object, all members of the base class are not visible. In private inheritance, the public and protected members of the base class can only be accessed by members of the directly derived class, not by objects, and cannot be inherited further down. 4. For the protection of inheritance mode:
• The visibility of the base class member to the derived class for the derived class,
The public and protected members of the base class are visible: the public and protected members of the base class are protected members of the derived class and can be accessed by other members, but not by the object of the derived class;
The private members of the base class are not visible: derived classes cannot access private members in the base class.
Therefore, for a derived class object, all members of the base class are not visible. When you protect inheritance, members of the base class can also be accessed only by other members of the directly derived class, not by objects, and cannot be inherited further down.
This type of inheritance is the same as the private inheritance approach. The difference between the two is only for the members of the derived class, but if the derived class continues to derive as the new base class, there is a distinct difference.
For example: Class B inherits the Class A in private, Class B derives new Class C, then other members of Class C and objects of derived classes cannot access members that are indirectly inherited from Class A (because they belong to a private member in Class B, and cannot access private members regardless of how the C class is derived)
If Class B inherits the Class A in a protected manner, Class B is derived from the new Class C (assuming it is inherited in public). Then other Members of Class C (except their objects) can access members that are indirectly inherited from Class A (because they belong to a protected member in Class B and derive the C class in the form of public inheritance). Just like a B-class public member can be accessed)