Access permission:
- Public public interface: open to the outside world and can be accessed through an object or Class Name
- Protected Access: the access permission is only available to sub-classes and cannot be accessed through objects.
- PRIVATE: Only class members are allowed to access the service.
Inheritance relationship:
- Public inheritance: the access permissions of the parent class members remain unchanged in the subclass, and they are still their original permissions.
- Protected inheritance: the access permission of the Public Member of the parent class is changed to protected in the subclass; the permissions of the protected and Private Members of the parent class remain unchanged in the subclass.
- Private inheritance: access permissions for all members of the parent classInChanges from subclass to private
Among them, public inheritance is the relationship between is-a. You can use the pointer of the parent class or reference the object pointing to the subclass; protected and private inheritance do not have the relationship between is-, it only indicates the relationship of "Combination" or "possession". It cannot use a pointer to the parent class or reference to point to the subclass object.
In the C ++ object model, the memory space of the subclass object contains the portion of the parent class. When the parent class pointer is used to point to the object of a subclass, this pointer can access the memory of the corresponding parent class. When protected and private inherit, the memory of the parent class is private and not open to the outside world, when protected and private are inherited, the pointer or reference of the parent class cannot be used to point to the subclass object.
Only public members can be accessed through objects.
When a class needs to be banned from being instantiated in the constructor form, you can declare the constructor of the class as private and protected. The constructor declares that classes in the form of protected cannot be directly instantiated, but can be inherited. Subclass can call the protected constructor of the parent class during instantiation.
The instantiation of classes declared as private by the constructor cannot depend on the constructor. You can provide the public instance () method, call the constructor In the instance () definition, and return the class instance. This principle is used in singleton mode.