Comparison of access permissions in public, protected, and private in C ++:
Public-defined members can be accessed anywhere;
Members defined by protected can only be accessed in the class and its subclass;
Private-defined members can only be accessed in the class itself.
In C ++, there are three types of inheritance relationships between classes: Public inheritance, protected inheritance, and private inheritance. What are the differences between these three inheritance types? First, let's take a look at the text description and give an example:
Public inheritance: when the class is inherited by the public, the access attributes of the public and protected members of the base class remain the same in the derived class, and Private Members of the base class cannot directly access the base class. That is to say, the access attributes of the public and protected members of the base class are not changed when they are inherited to the derived class. They are still the public and protected members of the derived class. Other members of the derived class can directly access them. In addition to the class family, you can only access the public members inherited from the base class through the objects of the derived class.
Private inheritance: when the class inheritance method is private inheritance, both the public and protected members in the base class appear in the derived class as private members, private Members of the base class cannot be directly accessed in the derived class. That is to say, the public and protected members of the base class are inherited and used as Private Members of the derived class. Other members of the derived class can directly access them, however, you cannot directly access them outside the class family through the objects of the derived class. Neither a member of a derived class nor an object of A derived class can directly access a private member inherited from a base class.
Protection inheritance: in inheritance, both the public and protected members of the base class are protected to appear in the derived class. Private member variables of the base class cannot be directly accessed. In this way, other members of the derived class can directly access the public and protected members inherited from the base class, but they cannot be directly accessed outside the class through the objects of the derived class, neither the member of the derived class nor the object of the derived class can directly access the private member of the base class.
I understand that in public inheritance, the member attributes in the base class remain unchanged, and Private Members in the base class cannot be accessed in the derived class, however, the public and protected members in the base class can be accessed in the derived class, but only the objects of the derived class can be used outside the derived class to access the public members of the base class, the private and protected members in the base class cannot be accessed outside the class of the derived class.
In private inheritance, Private Members of the base class cannot be directly accessed. The protected members and public members of the base class appear as private members in the derived class and can be accessed in the derived class, however, you cannot access the protection members and public members of the base class from outside the class of the derived class.
During inheritance protection, both the public and protected members of the base class appear in the derived class as protected members, while private members of the base class cannot access them directly, the derived class can directly access the public and protected members inherited from the base class, but the objects inherited from the derived class cannot directly access them. Neither the member of the derived class nor the object of the derived class can directly access the private member of the base class.