Differences among public, protected, and private in C ++: protectedprivate

Source: Internet
Author: User

Differences among public, protected, and private in C ++: protectedprivate

1. Access scope of private, public, and protected:

Private: It can only be accessed by functions in the class and their meta-functions. It cannot be accessed by any other function, nor can it be accessed by objects in the class.

Protected: it can be accessed by functions in the class, sub-class functions, and their friends functions, but cannot be accessed by objects in the class.

Public: can be accessed by functions in the class, sub-class functions, and their friends functions, or by objects in the class.

Note: youyuan functions include two types: global functions set as youyuan and member functions in youyuan class.

Second, method property changes after class inheritance:

Using private inheritance, all methods of the parent class are changed to private in the subclass;

Using protected inheritance, the protected and public methods of the parent class are changed to protected in the subclass, and the private method is not changed;

Using public inheritance, the method attributes in the parent class are not changed;

Three Access Permissions

Public: can be accessed by any entity

Protected: only access to child classes and member functions of this class is allowed.

Private: only access to member functions of this class is allowed.

Three inheritance Methods

Public inheritance

Protect inheritance

Private inheritance

Combination result

Subclass of the Inheritance Method in the base class

Public & public inheritance => public

Public & protected inheritance => protected

Public & private inheritance => private

Protected & public inheritance => protected

Protected & protected inheritance => protected

Protected & private inheritance => private

Private & public inheritance => The subclass has no access permission.

Private & protected inheritance => subclass does not have access permission

Private & private inheritance => The subclass has no access permission.

The preceding combination results show that

1. public inheritance does not change the access permissions of the base class members.

2. private inheritance changes the access permission of all members of the base class in the subclass to private

3. protected inheritance changes the public Member in the base class to the protected member of the subclass, And the access permissions of other members remain unchanged.

4. The private member in the base class is not affected by the inheritance method, and the subclass is never authorized to access it.

In addition, when private inheritance is used, access is permitted.

As we know, when the base class is inherited as private, its public and protected members become private members in the subclass. However, in some cases, you need to restore one or more inherited members in the subclass to their access permissions in the base class.

C ++ supports two methods to achieve this purpose

Method 1: Use the using Statement, which is recommended by the C ++ standard.

Method 2: Use the access declaration in the form of base-class: member;, where the appropriate access declaration is located in the subclass. (Note: You can only restore the original access permission, but cannot increase or decrease the access permission)

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.