1. Access Control Operators (three access attributes)
1. access controllers in the class: ① The default value is private and can be declared as public or protected;
② Access attributes of Private Members: they can only be accessed by member functions and user functions of the class, but cannot be accessed by other functions;
③ Access attributes of public members: they can be accessed by any function;
④ Protection (protected) member access attribute: in addition to being the same as a private member, it also allows function access by the member of the derived class, but does not allow access by other functions.
2. Access Controller in struct (struct): Public by default, private or protected
3. Union: Only public
That is, the member functions of this class (direct base class) have the right to access all members in the access control table. The member functions of a derived class can only access members with the public and protected attributes. Other functions can only access members with the public attribute.
2. Three inheritance Methods
The access relationship ing table composed of three access attributes is as follows:
Note: 1. the stack storage content of the derived class object has nothing to do with the inheritance method and access permission.
2. The access control table of a class only contains the data members and member functions that meet the access permission in the class and its base class (direct and indirect), rather than all members, it does not include virtual pointers.
The stack storage content of A Class Object includes all non-static data members of the class and all its base classes (direct and indirect) (whether accessible or not), as well as virtual pointers, it does not include any static data members or member functions. It has nothing to do with the inheritance method and access permission.
3. In some cases, when the data inherited from a derived class is private, you may want to change the data to a public one. You can use the following statement:
Public:
Using Base:;
Change the private nature of some inherited data in the derived class to public.
1 // der_priv_1.cpp 2 // when derivation is private so that public or protected member in base class 3 // becomes privately accessible in derived class, it can become publicly accessible 4 // By "using base: A;" 5 6 # include <iostream. h> 7 8 Class A 9 {10 public: 11 A (int I) {A = I;} 12 protected: // or public: 13 int A; 14 }; 15 16 class B: A // Private derivation17 {18 public: 19 B (int A): A (a) {} 20 using a: A; // set :: a's private nature changed to public nature 21}; 22 23 void main () 24 {25 B OBJ (5); 26 cout <obj. A <Endl; 27}