Member types in C ++ class

Source: Internet
Author: User

C ++ has three types of members: public members, Private Members, and protected members.
If this parameter is not specified, all the Members in the class are public and do not need to be declared. However, private members must be declared.

The difference between the three types of members is that for this class:
A public member that can be accessed externally.
Private member, which cannot be accessed from outside.
Protects members that cannot be accessed externally.

At first glance, the access permissions of Private Members are the same as those of the protected members. It seems that there is no need to add another protected member.
We need to believe that we think we are quite clear about the fact that C ++'s Daniel knows what is needed and what is useless if it is useless to protect members, will it still appear in C ++?
In fact, the difference between protecting members and private members lies in inheritance.

Inheritance can be divided into public inheritance, private inheritance, and protection inheritance.
Public inheritance: The public members of the base class and the protected members are public members of the derived class, which can be accessed externally;
Private inheritance: the public and protected members of the base class are private members of the derived class and cannot be accessed externally;
Protection inheritance: the public and protected members of the base class are protected members of the derived class and cannot be accessed externally;

(External refers to the external of the base class, that is, it can be accessed in the derived class, and cannot be accessed outside the derived class)

Among the three inheritance types, the derived classes cannot access the Private Members of the base class.
From inheritance, we can see that protecting members is essentially different from private members.
Of course, it can be said that there is no difference between protecting members and public members in inheritance.

It can be seen that protecting members is a third party between private members and public members. It is said that a third party may not be appropriate. It should be a husband in the wife of a husband who has a relationship with both sides, but in essence he is different from both sides (he is a man ...)

Through the above description, we can also see that the role of protecting members is mainly embodied in inheritance.

There may be good ideas. What is the difference between private Inheritance and Protection inheritance? The inherited members are externally inaccessible? There is no difference in inheriting a proper class, but if the derived class 2 is derived from the derived class 2, there is a difference, because Private Members cannot be accessed by the derived class 2 that is sent again.

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.