Part4. (two) permissions of member functions and other functions in the derived class to access base class members (access control table)

Source: Internet
Author: User
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}

 

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.