Access properties for C + + derived class members

Source: Internet
Author: User
Tags access properties

Since the derived class contains the members of the base class and the derived classes themselves, there is a problem with the relationship and access properties of the two-part members. When you build a derived class, you do not simply use the private members of the base class directly as private members of the derived class, and the public members of the base class directly as the public members of the derived class.


In fact, the addition of members to base class members and derived classes themselves is handled on a different principle. Specifically, there are a few things to consider when discussing access attributes:

    1. The member functions of the base class access the base class members.
    2. A member function of a derived class accesses a member of the derived class itself.
    3. The member functions of the base class access the members of the derived class.
    4. The member functions of the derived class access the members of the base class.
    5. A member of a derived class is accessed outside of a derived class.
    6. Accesses a member of a base class outside of a derived class.


For the cases of paragraphs (1) and (2), it is relatively simple that the member functions of the base class can access the base class members, and the member functions of the derived classes can access the derived class members. Private data members can only be accessed by member functions in the same class, and public members may be accessed by outsiders.


(3) It is also clear that the member functions of the base class can access only the members of the base class and not the members of the derived class.


The second (5) case is also more explicit, where the public members of the derived class can be accessed outside the derived class, and the private members of the derived class cannot be accessed.


For the cases of paragraphs (4) and (6), it is slightly more complicated and confusing. For example, a question was raised:

    • A member function in a base class can access any member of the base class, and the newly added members of the derived class can access the private members of the base class as well;
    • Outside a derived class, you can access a public member inherited from a base class through the object name of the derived class.


These involve the question of how to determine the access properties of a member of a base class in a derived class, not only the access properties declared for the base class member, but also the inheritance of the base class declared by the derived class, which together determine the access properties of the base class member in the derived class.


As mentioned earlier, in a derived class, the base class can inherit in 3 ways, public (common), private (private), and protected (protected). The different inheritance methods determine the access properties of the base class members in the derived class. Briefly, it can be summed up in the following points.


1) Common inheritance (public inheritance)
The public and protected members of the base class maintain the original Access property in the derived class, and their private members remain private to the base class.


2) Private inheritance (private inheritance)
The public and protected members of the base class are private members in the derived class, and their private members are still private to the base class.


3) Protected inheritance (Protected inheritance)
The public and protected members of the base class are protected members in the derived class, and their private members are still private to the base class. Protecting a member means that it cannot be referenced by the outside world, but can be referenced by members of the derived class.

Access properties for C + + derived class members

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.