Public protected private keyword in C ++

Source: Internet
Author: User
Tags protected constructor

Access permission:

    1. Public public interface: open to the outside world and can be accessed through an object or Class Name
    2. Protected Access: the access permission is only available to sub-classes and cannot be accessed through objects.
    3. PRIVATE: Only class members are allowed to access the service.

Inheritance relationship:

    1. Public inheritance: the access permissions of the parent class members remain unchanged in the subclass, and they are still their original permissions.
    2. 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.
    3. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.