C ++ public, protected, private

Source: Internet
Author: User
/* Function, C ++ public, protected, private date, March 13, 2013 environment, ubuntu1204-gcc blog, login <iostream> Using STD: cout; Using STD: Endl; template <typename T> void OK (t) {} Class A {// public can be accessed anywhere in the program, you do not need to use a member function // to directly access the instance of the class. Public: int pub;/* The protected class and its subclass can be accessed directly. That is to say, the base class contains the protected member. If the subclass inherits from the base class, you can also access the protected member of the base class, if the base class is a private member, the subclass is hidden and inaccessible. The protected access label is a combination of private and public: Like a private member, the protected member cannot be accessed by class users. Like a public member, a protected member can be accessed by a derived class of the class. A derived class can only access the protected member of its base class through a derived class object. The derived class has no special access permission to the protected member of its base class object. */Protected: int pro; // Private: It can only be accessed directly within the class. Private, class instances can be accessed through member functions. This // can hide information. PRIVATE: int PRV ;}; // inheritance of public, the base class members keep their own access level: the Public Member of the base class is the public // Member of the derived class, the protected member of the base class is the protected member of the derived class. Class A1: Public A {public: void F () {pub = 1; Pro = 2; // error: 'int :: prv' is a private // x prv = 3 ;}}; // protected inheritance. The public and protected members of the base class are protected members in the derived class. Class A2: Protected A {public: void F () {pub = 1; Pro = 2; // x prv = 3 ;}}; // Private inheritance, all the members of the base class are private members in the derived class. Class A3: Private A {public: void F () {pub = 1; Pro = 2; // error: 'int :: prv' is private // PRV = 3 ;}}; class A31: Public A3 {public: void F () {// X pub = 1; // X pro = 2; // x prv = 3 ;}}; int main () {A;. pub = 1; // error: 'int A: Pro' is protected // x. pro = 2; // error: 'int A: prv' is private // x. PRV = 3; OK (a); A1 A1; a1.pub = 1; // X a1.pro = 2; // X a1.prv = 3; a2 A2; // X a2.pub = 1; // X a2.pro = 2; // X a2.prv = 3; OK (A2); A3 A3 A3; // X a3.pub = 1; // X a3.pro = 2; // X a3.prv = 3; OK (A3 );}

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.