In the previous section, we saw the definition method of derived classes, using public inheritance. There are actually three methods here, namely public inheritance , private inheritance , and protected inheritance .
The main difference between different inheritance methods lies in the changes in the access rights of members with different access rights in the base class in the derived class. Introduced one by one below.
1. Public inheritance:
In the public inheritance mode, its characteristics are as follows:
(1) Public members in the base class are still public members in the derived class. Of course, both member functions and derived class objects in the derived class can be accessed.
(2) Private members in the base class cannot be accessed by members of the derived class or objects of the derived class.
(3) The protected members in the base class are still protected types in the derived class and can be accessed through the member functions of the derived class, but the derived class objects cannot be accessed.
2. Private inheritance:
In the case of private inheritance, the access rights of the three members of public type, private type, and protected type are as follows:
(1) The public and protected types of the base class, after being absorbed by the private inheritance of the derived class, become the private types of the derived class, that is, they can be accessed in the member functions of the class and cannot be accessed outside the class.
(2) Private members of the base class cannot be accessed in the derived class, whether inside or outside the class.
We can see that if it is a private derivation, the private members of the base class can no longer be used in the derived class or even the derived subclass, and have no meaning, so this kind of usage is relatively rare.
3. Protect inheritance:
Protected type inheritance has the following characteristics:
(1) Public members and protected type members of the base class are protected members in the derived class.
(2) Private members of the base class cannot be directly accessed in the derived class.
It can be seen that the member functions in the derived class can access the public members and protected members of the base class, but they cannot be accessed outside the class through the derived class object. Similarly, private members in the base class cannot be accessed through member functions in the derived class or through class objects.
The following is still taking the program code of clock and alarm clock as an example to experiment with different types of access issues derived from different permissions in the base class. For public types, they can be called in member methods of derived classes or outside the class. Compilation runs without errors as follows:
But for private members, we try to add a line of code see line 41. In the member function of the derived class, the H variable absorbed from the base class is assigned, and an error is reported during compilation. See the red box and the prompt below:
You can see the error message "H is a private type".
For protected types, we try to define a protected type variable w in the base class, and try to assign and use it in the publicly inherited derived class method. You can see the situation in the picture below, which is fully accessible. As shown below:
For private inheritance, members of public types are accessed outside the class:
You can experiment on your own to verify the access of different types of members under public inheritance.
After studying and experimenting, we can summarize as follows:
1. Private members of the base class cannot be accessed by member functions within the derived class or outside the class, regardless of derived permissions.
2. Private inheritance, no matter what the original type of the base class is, it cannot be accessed through member functions outside the derived class.
3. The access rights of members absorbed by the derived class from the base class are the lowest of the access rights in the base class and the derived rights at the time of derivation.
And based on the combination of derived permissions, permissions defined in the base class, and different access times within the derived class and outside the class, the following table is listed:
public inheritance | protected inheritance | private inheritance | ||||
Visit location | within class | Out of class | within class | Out of class | within class | Out of class |
public members | Can | Can | Can | Can't | Can | Can't |
protect members | Can | Can't | Can | Can't | Can | Can't |
private member | Can't | Can't | Can't | Can't | Can't | Can't |