Three access rules for derived classes to base class members in C + +

Source: Internet
Author: User

Derived classes in C + + have the following two types of access to base class Members:
1. Internal access: accessed by a member of a derived class that is inherited by a new member of the base class.
2. Object access: Outside the derived class, access to the members inherited from the base class through the objects of the derived class. Today we introduce the access rules of derived classes to base class members in the 3 inheritance mode.

1. Private inherited access rules
When a class inherits from a private inheritance, the public and protected members of the base class are inherited and become private members of the derived class, and other members of the derived class can access them directly, but the objects outside the class are inaccessible through the object of the derived class. A private member of a base class is not directly accessible in a privately derived class, so a private member inherited from a base class cannot be directly accessed either by a member of the derived class or by an object of the derived class, but can be accessed indirectly through the public member function provided by the base class. The access rules for private inheritance are summarized as follows:

Base class Members

Private members

Public members

Protected members

Internal access

Not accessible

Access to

Access to

Object access

Not accessible

Not accessible

Not accessible

2. Access rules for public inheritance
When a class is inherited by a public inheritance, the common members and protected members of the base class are inherited into the derived class and are still members of the publicly and protected of the derived class, and other members of the derived class can access them directly. However, an external consumer of a class can access inherited public members only through the objects of the derived class. The private members of the base class are not directly accessible in the privately derived class, so neither a derived class member nor an object of a derived class can directly access the private members inherited from the base class, but they are accessible directly through the public member functions provided by the base class. The access rules for public inheritance are summarized as follows:

Base class Members

Private members

Public members

Protected members

Internal access

Not accessible

Access to

Access to

Object access

Not accessible

Access to

Not accessible

3. Protection of inherited access rules
When a class inherits in a way that protects inheritance, the public and protected members of the base class are inherited into the derived class as protected members of the derived class, and other members of the derived class can access them directly, but the external consumer of the class cannot access them through the objects of the derived class. A private member of a base class is not directly accessible in a privately derived class, so a private member in the base class cannot be accessed directly, either by a derived class member or by an object of the derived class. The access rules for protection inheritance are summarized as follows:

Base class Members

Private members

Public members

Protected members

Internal access

Not accessible

Access to

Access to

Object access

Not accessible

Not accessible

Not accessible

Three access rules for derived classes to base class members in C + + (GO)

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.