C ++ class Access Control (Public/protected/private)

Source: Internet
Author: User

Original article link

 

Summary of public, protected, and private access labels in C ++ First, the access range of the private, public, and protected access labels. PRIVATE:It can only be accessed by 1. functions in this class, 2. Its membership functions. Cannot be accessed by any other user, nor can the object of this class be accessed.
Protected:It can be accessed by 1. functions in this class, 2. Sub-class functions, and 3. Its membership functions. But cannot be accessed by objects of this class.
Public:It can be accessed by 1. functions in this class, 2. Sub-class functions, 3. Friends functions, or 4. objects in this class. Note: Youyuan functions include three types: ordinary non-member functions set as youyuan; member functions of other classes set as youyuan; all member functions in youyuan class.

2: Changes in method attributes after class inheritance. private attributes cannot be inherited.
use private inheritance , the protected and public attributes of the parent class are changed to private in the subclass.
use protected Cheng, the protected and public attributes of the parent class are changed to protected in the subclass;
use Public inheritance. the protected and public attributes in the parent class are not changed; as follows:

  Public: Protected: PRIVATE:
Public inheritance Public Protected Unavailable
Protected inheritance Protected Protected Unavailable
Private inheritance Private Private Unavailable
Protected inheritance and private inheritance can reduce access permissions.

End

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.