Control of the access rights of class members in C + + public inheritance, protection inheritance, and private inheritance __c++

Source: Internet
Author: User

Zz:http://blog.sina.com.cn/s/blog_b35e31b90101b6y7.html

To prevent the connection from being invalidated, turn it back directly.


Many students still have access to the right is not very clear, or more vague, some students remember and forget, forget and remember, so today I want to borrow this article to do some summary, and then provide a relatively simple way to remember the access rights in C + +.

As we all know, C + + through public,protected,private three keywords to implement class members (including member variables and member methods) control access, the following to facilitate memory, I give access to the size of the definition: public > Protected > Private > No access, based on this definition gives me a summary of a small trick, that is: In the event of a rise in the fall, the fall, flat is flat, except for private. The following illustration of access rights:

For the convenience of everyone to understand, give an explanation:
1. When a member variable has access to private in the parent class, regardless of the inheritance, the access rights in the subclass become no access, which is called private exception.
2, when a member variable has access to protected in the parent class, when the inheritance is public, the access rights in the subclass are protected, which is called a rise-and-rise, protected, and the access rights in the subclass are still protected, which is called flat, When the inheritance is private, the way of accessing the child class becomes private, which is called descending.
3, when the member variable in the parent class access is public, when the inheritance is public, the access rights in the subclass is public, flat is flat; When the inheritance is protected, the access rights in the subclass are protected, and the drop is dropped. When the inheritance mode is private, the access rights in the subclass are private, and the drop is dropped.

Through the above summary we also found that the drop is reduced, is always a encounter with the same, for example, when the public encountered private, access to become private, when encountered protected, it becomes protected.
Having understood the rules in the table above, we can easily use this access authority mechanism to write programs, before we have to understand that, a class members of the access rights are determined, and corresponding to two cases, one is in the class, one is outside the class. To sum up:
Inside the class: As long as it is not no access, we can access that member. In other words, the access rights within the class is relatively loose, and all accessible permissions can be accessed, whether public, protected or private.
Outside the class: we are not allowed to access the member as long as it is not public. That is, the external access to the class is more restrictive, as long as the access permission is not a public member and cannot be accessed outside the class.

Do you understand.
In order to let everyone again profound understanding of the above summary, I also designed for you the following small test procedures: hope that the students themselves hands-on test:

Class Animal//Parent class
{
Public
void Eat () {
cout<< "Animal Eat" <<endl;
}
Protected
void Sleep () {
cout<< "Animal Sleep" <<endl;
}
Private
void Breathe () {
cout<< "Animal Breathe" <<endl;
}
};

Class Fish:public Animal//Subclass
{
Public
void Test () {
Eat (); At this point, the access rights of Eat () are public and can be accessed within the class
Sleep (); The access to sleep () at this time is protected and can be accessed within the class
Breathe (); At this point breathe () has access to no access and cannot be accessed within the class

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.