Discussion on "turn" C + + class access control public/private/protected

Source: Internet
Author: User

Example 1:----------------------------------------

Class c{
int C1;
int C2;
Public
void Set (c* s, int i, int j)
{
S->C1 = i;
S->C2 = j;
}
Friend std::ostream& operator<< (std::ostream& os, const c& C);
};
std::ostream& operator<< (std::ostream& os, const c& C)
{
os<<c.c1<< "" <<c.c2<<endl;
return OS;
}

C A;
C b;
A.set (&a, 1, 2);
A.set (&b, 3, 4);

cout<<a;
cout<<b; Output 1 2

3 4

Example 2:--------------------------------------------------

#include <IOSTREAM>
using namespace Std;

Class a{
Public
A (int i_,int j_)
{
I=i_;
J=j_;
}
void disp (A &a)
{
cout<<a.i<<endl<<a.j<<endl;
}

Private
int i;
Protected
Int J;
};

int main (int argc, char* argv[])
{
A (123,456);
A b (789,543);
A.disp (b);
B.disp (a);

return 0;
}

-------------------------------------------------------

A class is a wrapper around a data member and a series of operations (member functions) on it, noting that member functions can manipulate data members!


Object is an instantiation of a class, how do you understand instantiation? In fact, each instance object simply initializes the data members in it, and each object in the memory image retains only the copy of the data member that belongs to it. The member function is shared for the entire class, that is, a class retains only one copy of the member function.
So how does each object relate to these member functions that can be thought of as "detached", that is, how member functions manipulate the data members of an object? Remember this pointer, regardless of the object passing (.) Operation or (-) Action call member function, compile time, the compiler will convert this call to our common form of global functions, and a parameter (usually this parameter is placed in the first), and then pass the this pointer to this parameter. The binding (or contact) of the object to the member function is completed.
After instantiation, you get multiple different objects of the same class, and since member functions are shared, the member function can manipulate the data members of the object.


The problem is that there are now multiple objects, and the member function needs to know which object's data member is being manipulated?
For example, there are objects obj1 and obj2, all belong to Class A, class A has public member function foo ()
If the function is called by obj1, the Foo function is passed to the this pointer at compile time, and the member of the Obj1,foo operation Obj1 itself is directly accessed without any modification, because the data members are automatically found based on the this pointer.
If Obj1 calls the function, it can also access data members of other objects of the same kind! So what you need to do is let the Foo function know which object's data members are in the same object, and one solution is to pass in pointers or references to other objects of the same kind, so that you can manipulate the data members of other objects of the same kind.
Foo (A &obj)
This is defined and then called:
Obj1.foo (OBJ2)
You can access OBJ2 data members in Obj1, regardless of whether the data members are private or protected


Summary: Thepurpose of the C + + access modifier is to be in a class and not in an object unit.

In layman's words, similar objects can "access" each other's data members , but the access path is not directly accessible.
The step is to call its public member function through an object that can access the Public/private/protected data members and member functions (all objects of the class) to its own or other objects of the same type, and also to indicate which object's data member ( The object that invokes the function does not have its own members specified, because there is a this pointer; data members of other objects can be specified indirectly by reference or pointer)

------------------------------------------------------------------------------

Public,protected,private Access Summary in C + +
First: The access scope of the Private,public,protected method. (Under public inheritance)
Private: can only be accessed by a function in the class, its friend function, and cannot be accessed by any other object of the class.
Protected: can be accessed by functions in the class, functions of subclasses, and their friend functions, but cannot be accessed by objects of that class
Public: can be accessed by functions in the class, by functions of subclasses, by their friend functions, or by objects of that class
Note: The friend function consists of two kinds: global function set as friend, set as member function in friend class

Second: Post-Inheritance Method property changes for classes:
With private inheritance, all methods of the parent class become private in subclasses;
With protected inheritance, the protected and public methods of the parent class change to Protected,private method in the subclass;
Using public inheritance, the method attribute in the parent class does not change;

Public Protected Private
Public inheritance Public Protected ---
Protected inheritance Protected Protected ---
Private inheritance Private Private ---

-----------------------------------------------

We often hear the saying that:

1) A class friend can access any member of the class (including member variables and member methods, hereinafter).
2) Private members only have access to the class itself, and protected members are accessible only to the class and its derived classes, and to all members of the public.

Who is the object (patient) This is very clear, is a member of the class (including member variables and member methods). who is the subject (agent)? This is the key point to confuse people. is also a vague part of the argument.

To be clear, the subject (agent) refers to a function , not a class (and certainly not a variable). Private/public/protected is to control the access of a function (agent) to members of a class, including member variables and member methods . So the more complete statement is:

1) A class friend (containing all member functions of a friend function or friend Class) can access any member of the class (including member variables and member methods).

2) Except for friends, private members can only access the member functions of the class itself , protected members only member functions of the class and member functions of their derived classes can be accessed, public members are accessible to all functions.

That is, when we say a class can access xxx, actually alluding to this class of member functions can access XXX. With this in mind, and with an obvious rule, the above question is not difficult to answer. This rule is:

3) Derived classes can weaken the access rights of members (through protected/private adornments) when they inherit. For example, the above example class P:protected O {...}; When a function accesses a member of O through Class p, the function has only protected permissions on the public member in Class O.

------------------------------------------------------------

Appendix:

Http://www.cnblogs.com/chio/archive/2007/06/11/779408.html

http://blog.csdn.net/crayondeng/article/details/14498539

Discussion on "turn" C + + class access control public/private/protected

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.