Simple Introduction: private/public/protected in C ++

Source: Internet
Author: User

The following question is taken from a forum post:

Three classes O, P, and q are known. Class O defines a private method F1, a public method F2, and a protected method F3: class P and class Q are derived classes of Class O. The Inheritance Method is as follows:
Class P: protected o {...};
Class Q: Public o {...};
The correct description of method F1 is ___ (34) ___; the correct description of method F2 is ___ (35 )___; which of the following statements about method F3 is correct? ___ (36 )___.
(34)
A. Method F1 cannot be accessed
B. Method F1 can be accessed only within Class O
C. Method F1 can be accessed only within Class P
D. Access Method F1 only within Class Q
(35)
A. Objects of Class O, P, and Q can access the F2 method.
B. Objects of the p and q classes can access the F2 method.
C. Objects of the Class 0 and Q can access the F2 method.
D. Access Method F2 only within Class P
(36) A. Objects of Class 0, P, and Q can access method F3.
B. Objects of the 0, P, and Q classes cannot access method F3.
C. Objects of the Class 0 and Q can access the method F3.
D. Objects of the class p and q can access method F3.

Is there any way to simply remember these many rules? The following describes a method that you do not need to remember.

As the name suggests, private/public/protected indicates private/public/Protection respectively. They are a group usedAccess permissionControl keywords. First, the key point to be clarified is:Control who is authorized to access? Who is the subject of this access? Who is the object (subject?

We often hear this saying:

1) A ClassYouyuanYou can access anyMember(Including member variables and member methods, the same below ).
2) PrivateMemberOnly this class can be accessed, protectedMemberOnly the class and its derived class can be accessed, publicMemberAll users can access the service.

It is clear who the object (subject) is.Member(Including member variables and member methods ).Who is the subject? This is the key to obfuscation. It is also vague in this statement.

To be clearSubject)Refers toFunctionInsteadClass(Not a variable, of course ). Private/public/protected:A function)ForClass member (including member variables and member methods). Therefore, the complete statement is:

1) A ClassYouyuan (including all member functions of youyuan function or youyuan class)You can access anyMember(Including member variables and member methods ).

2) except for friends, privateMemberOnlyMember functions of this classAccessible, protectedMemberOnly the member functions of this class and Their Derived classes can be accessed, publicMemberAll functions are accessible.

That is to say, when we say that a class can access XXX, it actually impliesMember functions of this classYou can access XXX. After understanding this and adding an obvious rule, it is not difficult to answer the above questions. This rule is:

3) when the derived class inherits, the access permission of the member can be weakened (modified by protected/private ). For example, the above example class P: protected o {...}; When a function accesses a member in O through class P, this function has only the protected permission for the Public Member in Class O.

Supplement: There is a technology called member spy (class member spyware). Through this technology, a derived class can be used to change the protected member of the base class to the public permission. This technology uses the using keyword. Example:

Class
{
Protected:
Int m_data;
};

Class spya: public
{
Public:
Using a: m_data;
};

Void testspy (A * pA)
{
Spya * pspya = static_cast <spya *> (PA );
// Forcibly convert a to spya, which requires that spya has no member variables and does not overload the virtual function in.
// Now you can access m_data through pspya. For example, int DATA = pspya-> m_data;
}

Because this technology uses forced type conversion, use it with caution.

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.