Public,protected,privat differences

Source: Internet
Author: User

About the protection of methods and properties inherited from base classes:

--class pig:public Animal {...}

C + + Not only allows you to enforce access control over the methods and properties defined in the class, but also allows you to control which methods and properties in the base class can be accessed by subclasses.

Public

-is telling the compiler that there is no change in the access level of inherited methods and properties, that public can still be accessed by all code, that protected can only be accessed by subclasses of the base class, and private only by the base class itself.

Protected

-Change the access level of the base class to protected, and if it is public, this will make it impossible for code outside the subclass to access public in the base class through subclasses.

Private

-is to tell the compiler that each member inherited from the base class is treated as private, which means that only this subclass can use the elements it inherits from the base class.

Note: Generally only use public!

  

Public,protected,privat differences

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.