Original article link
Summary of public, protected, and private access labels in C ++
First, the access range of the private, public, and protected access labels. PRIVATE:It can only be accessed by 1. functions in this class, 2. Its membership functions. Cannot be accessed by any other user, nor can the object of this class be accessed.
Protected:It can be accessed by 1. functions in this class, 2. Sub-class functions, and 3. Its membership functions. But cannot be accessed by objects of this class.
Public:It can be accessed by 1. functions in this class, 2. Sub-class functions, 3. Friends functions, or 4. objects in this class. Note: Youyuan functions include three types: ordinary non-member functions set as youyuan; member functions of other classes set as youyuan; all member functions in youyuan class.
2: Changes in method attributes after class inheritance. private attributes cannot be inherited.
use private inheritance , the protected and public attributes of the parent class are changed to private in the subclass.
use protected Cheng, the protected and public attributes of the parent class are changed to protected in the subclass;
use Public inheritance. the protected and public attributes in the parent class are not changed; as follows:
|
Public: |
Protected: |
PRIVATE: |
Public inheritance |
Public |
Protected |
Unavailable |
Protected inheritance |
Protected |
Protected |
Unavailable |
Private inheritance |
Private |
Private |
Unavailable |
Protected inheritance and private inheritance can reduce access permissions.
End