Source: http://www.cppblog.com/woaidongmao/archive/2010/07/01/119024.html
In C ++, a derived class can access base class members in the following two forms:
1. Internal access: Access to members inherited from the base class by new members in the derived class.
2. Object Access: Access to members inherited from the base class through the object of the derived class outside the derived class. Today, we will introduce the access rules of the derived class on the base class members in the inheritance mode of 3.
1. Private inherited access rules
When the class inheritance method is private inheritance, the public and protected members of the base class are inherited and become private members of the derived class. Other members of the derived class can directly access them, however, the object of the derived class cannot be accessed outside the class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member inherited from the base class, however, it can be indirectly accessed through the Public member functions provided by the base class. The access rules of private inheritance are summarized as follows:
Base class member |
Private member |
Public Member |
Protected Member |
Internal access |
Inaccessible |
Accessible |
Accessible |
Object Access |
Inaccessible |
Inaccessible |
Inaccessible |
2. access rules inherited by the Public
when the class is inherited by the public, the public and protected members of the base class are inherited to the derived class and still serve as the public and protected members of the derived class. Other members of the derived class can directly access them. However, the external user of the class can only access the inherited public member through the object of the derived class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member inherited from the base class, however, you can directly access them through the Public member functions provided by the base class. The public-inherited access rules are summarized as follows:
Base class member |
Private member |
Public Member |
Protected Member |
Internal access |
Inaccessible |
Accessible |
Accessible |
Object Access |
Inaccessible |
Accessible |
Inaccessible |
3. Protection of inherited access rules
When the class inheritance method is to protect inheritance, the public and protected members of the base class are inherited to the derived class as the protected member of the derived class. Other members of the derived class can directly access them, however, the external users of the class cannot access them through the objects of the derived class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member of the base class. The inherited access protection rules are summarized as follows:
Base class member |
Private member |
Public Member |
Protected Member |
Internal access |
Inaccessible |
Accessible |
Accessible |
Object Access |
Inaccessible |
Inaccessible |
Inaccessible |