Difference between public/private/protected ACL, privateprotected

Source: Internet
Author: User

Difference between public/private/protected ACL, privateprotected

// Differences between public/private/protected access control permissions
// Time: 2016/8/16 // (1) modifier: // public: can be used both inside and outside the class. // Protected: Used in the class and inherited subclass. // Private: it can only be used inside a class. // (2) modify the inheritance relationship: // public: The member of the subclass can access the public/protected member of the parent class. // The subclass object can access the public member of the parent class. // Private: The subclass member can access the public/protected member of the parent class. // The subclass object cannot access any member of the parent class. // Protected: The subclass member can access the public/protected member of the parent class. // The subclass object cannot access any member of the parent class. # Include "stdafx. h "# include" iostream "using namespace std; class Parent {public: int a; // the old name protected: int B; // the old bank password private: int c; // The father's lover}; // the inheritance of protection, family. Class Child1: protected Parent {public: protected: public: void useVar () {a = 0; // public OK B = 0; // protected OK // c = 5; // Private not OK }}; // Private inheritance, common. Class Child2: private Parent {public: void useVar () {a = 0; // public OK B = 0; // protected OK // c = 0; // Private not OK }}; // public inheritance, common. Class Child3: public Parent {public: void useVar () {a = 0; // public OK B = 0; // protected OK // c = 0; // Private not OK }}; int main () {Child1 c1; // protection inheritance, family. Child2 c2; // Private inheritance, common. Child3 c3; // common inheritance. // C1.a = 6; // Private not OK // c2.a = 6; // protection not OK c3.a = 6; // public OK return 0 ;}

 

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.