Explanation of private protected public:

Source: Internet
Author: User
1 # include <iostream> 2 # include <stack> 3 # include <queue> 4 # include <exception> 5 # include <sstream> 6 # include <string> 7 # include <vector> 8 # include <list> 9 # include <cassert> 10 # include <cstdlib> 11 using namespace STD; 12 13 14 Class A {15 private: 16 int aprivatevar; 17 void apirvate () 18 {19 cout <"A: protected vate ()" <Endl; 20} 21 protected: 22 int aprotectedvar; 23 void aprotected () 24 {25 cout <"A: aprotected ()" <Endl; 26} 27 public: 28 int apublicvar; 29 void apublic () 30 {31 cout <"A: apublic ()" <Endl; 32} 33 34 void test () 35 {36 aprivatevar = 1; 37 apirvate (); 38 aprotectedvar = 1; 39 aprotected (); 40 apublicvar = 1; 41 apublic (); 42} 43}; 44 45 class bprivate: Private a 46 {47 public: 48 void test () 49 {50 // access the base class private member 51 // aprivatevar = 1; // here, because the subclass declares private, 52 // apirvate () is not accessible. // here, 53 54 aprotectedvar = 1 is not accessible because the subclass declares private; 55 aprotected (); 56 57 apublicvar = 1; 58 apublic (); 59} 60}; 61 62 class cprivate: Public bprivate 63 {64 public: 65 void test () 66 {67 // none can be accessed here 68 // In the subclass: the public and protected members of the base class are visible. both the public and protected members of the base class become private members of the subclass, and cannot be accessed by the derived class of this subclass. 69/* effecvatevar = 1;/70 apirvate ();/71 72 aprotectedvar = 1; 73 aprotected (); 74 75 apublicvar = 1; 76 apublic (); */77} 78}; 79 80 Class bprotected: Protected A 81 {82 public: 83 void test () 84 {85 // access the base class private member 86 // aprivatevar = 1; // here, because the subclass declares private, it cannot access 87 // apirvate (); // here, because the subclass declares private, it cannot be accessed 88 89 aprotectedvar = 1; 90 aprotected (); 91 92 apublicvar = 1; 93 apublic (); 94} 95 }; 96 97 98 class bpublic: public a 99 {100 public: 101 void test () 102 {103 // aprivatevar = 1; // The subclass cannot access the private member 104/apirvate () in the base class; // The subclass cannot access the private member 105 106 aprotectedvar = 1 in the base class; // The subclass can access the protected member 107 aprotected () in the base class; // The subclass can access the protected member 108 109 apublicvar = 1 in the base class; // sub-classes can access the public member 110 apublic () in the base class; // sub-classes can access the public member 111} 112} in the base class; 113 114 int main (INT argc, char * argv []) 115 {116 a testa; 117 // Testa. apirvate (); // The Private method of the Instance cannot access 118 // Testa. aprotected (); // The instance's protected method cannot access 119 Testa. apublic (); 120 121 bprivate testb; 122 // testb. apirvate (); // The Private method of the Instance cannot access 123 // testb. aprotected (); // The instance's protected method cannot access 124 // testb. apublic (); // It is inherited by private. so. the instance object cannot access the private method 125 126 bprotected testb2; 127 // testb2.apirvate (); // The Private method of the Instance cannot access 128 // testb2.aprotected (); // The instance's protected method cannot access 129 // testb2.apublic (); // The instance's protected method cannot access 130 131 132 bpublic testb3; // testb3.apirvate (); // subclass instance object. only public members of the base class can be accessed. 133 // testb3.aprotected (); // Instance Object of the subclass. only public members of the base class can be accessed. 134 testb3.apublic (); // Instance Object of the subclass. only public members of the base class can be accessed. 135 return 0; 136} 137 138/* 139 conclusion: 140 the Instance Object of the base class is the same as that of the common class, only public member methods and Public member variables of the values class can be seen; that is, public members are visible, and protected and private are invisible; 141 1. public inheritance: the public and protected members of the base class are visible to the subclass. after the inheritance, both public and protected remain in the original state. private is invisible. that is to say, the private member of the base class cannot be accessed in subclass 142, And the Instance Object of the 143 subclass. only public members of the base class can be accessed. and its own public members. 144 so: when the public class is inherited, the Instance Object of the subclass can access the public member in the base class. the member methods in the subclass can access the public and protected members in the base class. 145 2. private inheritance: the Instance Object of the base class, which can access the public members of the base class (same as that of the normal class ). 146 in the subclass: the public and protected members of the base class are visible. both the public and protected members of the base class become private members of the subclass. These public and protected members cannot be accessed by the derived classes of this subclass. 147 The Private Members of the base class are invisible, that is, the subclass cannot access the Private Members of the base class. 148. The instance object of the subclass cannot access all the members of the base class. during private inheritance, the members of the base class can only be accessed by the direct derived class, but cannot be inherited further. 149 3. protected inheritance: The subclass can access the public and protected members of the base class. Both the public and protected members of the base class become protected members and cannot be accessed by the subclass of the derived class, the private member 150 of the base class is invisible, and the derived class cannot access the private member of the base class. when protected is inherited, the members of the base class can only be accessed by the direct derived class and cannot be inherited. 151 152 */

 

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.