C ++ Primer 4th Object-Oriented Programming Reading Notes --- protected member

Source: Internet
Author: User

I haven't looked at the basic knowledge of C ++ for a long time and forgot many things. I am not sure about the C ++ class protected in other places today. I flipped through "C ++ Primer 4th" and found that there are more confusing places. It is actually a waste of capabilities. The foundation is really not good.

------------------ Split line ---------------------

Class Design and protected members
If there is no inheritance, there are only two types of users in the class:Members of the class and users of the class. Dividing classes into private and public access levels reflects the separation of user types:Users (programs using this class, including class objects) can only access public interfaces. class members and friends can both access public members and private members.
With inheritance, there is the third user of the class:A programmer (derived class) that defines a new class from a class ).The provider of a derived class usually (but not always) needs to access (generally private) The base class implementation. In order to allow such access, general access to the implementation is still prohibited, an additional protected access label is provided. The protected part of the class cannot be accessed by General programmers (users using this class), but can be accessed by the derived class. Only the class itself and its friends can access the private part of the base class. The derived class cannot access the private member of the base class.
When defining a class to act as a base class, the standard for designing the member as public has not changed: the interface function should still be public, and the data should not be public. The inherited class must determine which parts are declared as protected and which parts are declared as private. If you want to disable access to a derived class, you should set it to private. The members that provide the derived class to implement the required operations or data should be set to protected. In other words, the interface provided to the derived type is a combination of protected members and public members.


Protected Member

It can be considered that the combination of private and public in the protected access label.

1. Like private members, protected members cannot be accessed by class users.

2. Like a public member, a protected member can be accessed by a derived class of this class.

In addition, protected has another important nature.

You can useDerived class ObjectAccess the protected member of the base class.Base class type objectThe protected member of does not have special access permissions.

With the code understanding is probably the following (original code link: http://blog.csdn.net/luoruiyi2008/article/details/7179788 ):

# Include <iostream> using std: cout; using std: endl; class Base {public: Base () {}; virtual ~ Base () {}; protected: int int_pro;}; class A: public Base {public: A () {}; A (int da) {int_pro = da ;} // The derived class (A) can directly access the protected member (int_pro) void Print (A & obj) {obj.int _ pro = 24;} of the Base class ;} // The derived class Object (obj) can access The protected member (int_pro) void PrintPro () {cout <"The proteted data is" <int_pro <endl;} of The base class ;} // The derived class (A) can directly access the protected member (int_pro) void PrintBasePro (Base & obj) of the Base class (Base) {cout <"The protected data is" <obj.int _ pro <Endl;} // The derived class (A) has no special access permission on the base class Object (obj) protected member (int_prot). Therefore, the code compilation fails when this sentence is added .} ; Int main (void) {A aObj; A aObj2 (5); aObj2.PrintPro (); aObj. Print (aObj2); aObj2.PrintPro (); return 0 ;}

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.