There are three types of access control for C + + classes: public,protected and private.
Class access control |
What objects a class member can be interviewed by |
Public |
1. The member function of the class. 2, Class object. 3, friend Yuan. 4. Sub-class member functions |
Protected |
1. The member function of the class. 2, friend Yuan. 3. Sub-class member functions |
Private |
1. The member function of the class. 2, friend Yuan |
Ii. Inheritance of C + + classes
There are also three ways of inheriting: public,protected and private.
After inheritance, the accessibility of base class members changes, such as the following table:
Public inheritance
Base class |
Public |
Protected |
Private |
Sub-class |
Public |
Protected |
Not visible |
Protected inheritance
Base class |
Public |
Protected |
Private |
Sub-class |
Protected |
Protected |
Not visible |
Private inheritance
Base class |
Public |
Protected |
Private |
Sub-class |
Private |
Private |
Not visible |
It can be seen from the above that, after inheritance, the access of the base class members is changed, and the accessibility of the derived class is still subject to the class's control rules. About three types of inheritance, derived classes to the base class members of the interview, there are very large online netizens wrote a large number of articles, but I think the text is very easy to describe the person around the halo, but lost its essence. I thought the inheritance is not so troublesome as the Netizen says, that is, the accessibility of the base class has changed in the derived class, and then the derived class can then use the Class access control rule to interview it.
There are two notable places to note:
The private members in the base class are not visible in the derived class, that is, they cannot be interviewed by sending their own member functions, but can be visited through the inherited member functions.
Second, private inheritance, both the public and protected members of the base class become private members of the derived class, so that if a subclass is derived from the derived class, then, in the derived subclass, it is not possible to access the inherited member directly through the member function of the subclass;
Third, which members of a derived class cannot inherit from a base class
There are three members of a derived class that cannot inherit from a base class:
1. Constructors and destructors
2. Assignment operator (operator= () member)
3, friend Yuan
C + + Class interview control and inheritance