When private, public, and protected are set as members of a class:
PRIVATE: It can only be accessed by functions in the class and their meta-functions. It cannot be accessed by any other function, nor can it be accessed by objects in the class.
Protected: can be accessed by functions in the class, sub-class functions, and their friends functions,
Objects of this class are not allowed.Access
Public: can be accessed by functions in the class, sub-class functions, and their friends functions, or by the class objects. Note: Friends functions include two types: global functions set as friends functions, set as a member function in the youyuan class
When private, public, and protected are used as inheritance methods:
For public inheritance:
(1) The Public Member of the parent class becomes the Public Member of the subclass, allowing code other than the class to access these members;
(2) The Private member of the parent class is still the private member of the parent class, and the Child class members cannot access these members;
(3) The protected member of the parent class becomes the protected member of the subclass and can only be accessed by the member of the subclass;
(1) base class members
ObjectVisibility:
Public members are visible, while others are invisible. Here, the protection of members is the same as that of private members.
(2) Base-class member Pairs
Derived classVisibility:
Public and protected members are visible, while private members are invisible. The members are protected in the same way as public members.
(3) Base-class member Pairs
Derived class ObjectVisibility:
Public members are visible, while other members are invisible.
Therefore, in public inheritance, the object of the derived class can access the public members in the base class; the member functions of the derived class can access the public members and protect members in the base class. Here, you must distinguish between the object of the derived class and the member functions in the derived class to access the base class.
For private inheritance:
(1) The Public Member of the parent class becomes the private member of the subclass and can only be accessed by the subclass member;
(2) The Private member of the parent class is still the private member of the parent class, and the Child class members cannot access these members;
(3) The protected member of the parent class becomes the private member of the subclass and can only be accessed by the subclass member;
(1) base class members
ObjectVisibility:
Public members are visible, while other members are invisible.
(2) Base-class member Pairs
Derived classVisibility:
Public and protected members are visible, while private members are invisible.
(3) Base-class member Pairs
Derived classObject visibility:
All members are invisible.
Therefore, in private inheritance, the base class members can only be accessed by direct Derived classes, but cannot be inherited.
For the protection Inheritance Method:
(1) The Public Member of the parent class becomes the protected member of the subclass and can only be accessed by the subclass member;
(2) The Private member of the parent class is still the private member of the parent class, and the Child class members cannot access these members;
(3) The Public Member of the parent class becomes the protected member of the subclass and can only be accessed by the subclass member;
This inheritance method is the same as the private inheritance method. The difference between the two lies in that they have different visibility on the base class members for the members of the derived class.
The visibility mentioned above is accessibility. There is another saying about accessibility. In this rule, the object of the derived class uses horizontal access to the base class, and the access of the derived class to the base class is vertical access.
The general rules are as follows:
Horizontal access and vertical access are not restricted to public members in the base class during public inheritance;
During private inheritance, horizontal access and vertical access cannot be accessed by public members in the base class;
During inheritance protection, vertical access is the same as public inheritance, and horizontal access is the same as private inheritance.