Public, private, and protected are common inheritance methods.
1. Public inheritance)
Public inheritance is characterized by the original state of both the public members of the base class and the protected members as the members of the derived class, while the private members of the base class are still private, it cannot be accessed by the subclass of this derived class.
2. Private)
Private inheritance features that the public and protected members of the base class are both private members of the derived class and cannot be accessed by the subclass of the derived class.
3. Protected)
The protection inheritance feature is that all the public and protected members of the base class become the protected members of the derived class and can only be accessed by the member functions or friends of the derived class, the Private Members of the base class are still private.
The following lists the basic class features and derived class features of three different inheritance methods.
|
Public |
Protected |
Private |
Total inheritance |
Public |
Protected |
Invisible |
Private inheritance |
Private |
Private |
Invisible |
Protection inheritance |
Protected |
Protected |
Invisible |
In: 1) The base class members are visible to the common and protected members and Private Members are invisible to the derived classes.
2) base class members for the objects of the derived class: It depends on the type of members of the base class in the derived class. For example, in private inheritance, both the common and Private Members of the base class become private members in the derived class, therefore, for objects in a derived class, the common and Private Members of the base class are invisible.
To further understand the differences between the three inheritance methods in terms of Member visibility, we will discuss them from three different perspectives.
For public inheritance
(1) Visibility of base class members on their objects:
Public members are visible, while others are invisible. Here, the protection of members is the same as that of private members.
(2) Visibility of base class members on Derived classes:
Public and protected members are visible, while private members are invisible. The members are protected in the same way as public members.
(3) Visibility of base class members on derived class objects:
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 Methods
(1) Visibility of base class members on their objects:
Public members are visible, while other members are invisible.
(2) Visibility of base class members on Derived classes:
Public and protected members are visible, while private members are invisible.
(3) Visibility of base class members on derived class objects:
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
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.
This article Reprinted from: http://www.360doc.com/content/11/1030/11/7940675_160259440.shtml #