(Note): access restriction for combination and inheritance (1), and inheritance
Before introducing combination and inheritance, we will first introduce access restrictions. Access Restrictions: public, private, and private are sorted by authorization size. Here is a blog that provides a classic interpretation of the three.
Http://blog.csdn.net/artechtor/article/details/2295739
The following describes public and private, while protected is talking about inheritance.
Public: the internal objects of A Class are all public. Its access domain can be used anywhere, for example, Class A and. use any public variables and functions in the class. This is a simple example.
Private: (using member variables and member functions) can be accessed only by the same class.
This statement can be accessed only through the member functions of the class (the scope is only in the Declaration section of the class ). So even if we use Class A to define an object in other functions. ii. private member variables cannot be accessed, that is, private variables can only be accessed through member functions (directly or indirectly using member functions ).
First, you can directly use an object call (this class can operate on private member variables or private member functions) to access the class functions or variables of private attributes.
Type 2: a similar object can call the public member function of the class (for the pointer to the class) to access the private member variable of another object of the same class, and then access the private member variable of this class.
The following are two examples.
First:
The code and result are as follows:
1 # include <iostream> 2 using namespace std; 3 4 class A {5 private: 6 int I; 7 public: 8 A (): I (10) {} 9 void print () {10 cout <". I = "<I <endl; 11} 12}; 13 14 int main () 15 {16 A; 17 //. i; // If you cancel the annotation here, the compilation error will prompt that I is a private variable 18. print (); 19 20 return 0; 21}
View Code
Result:
Second:
1. An object can access the private variable of another object through a member function (the parameter is a pointer to the same class. The Code is as follows:
1 # include <iostream> 2 using namespace std; 3 4 class A {5 public: 6 int I; 7 A (): I (10), j (20) {} 8 void print (A * p) {9 p-> j = 50; 10 cout <"private Variable p-> j =" <p-> j <endl; 11} 12 private: 13 int j; 14 }; 15 16 int main () 17 {18 A; 19 a B; 20. print (& B); // This is the private member variable used to access the same class through the member function, 21 // This method can also modify the value of the private variable pointed to by the corresponding object 22}
View Code
Result:
2. One object cannot access the private variable of another heterogeneous object through a member function (the parameter is a pointer to a heterogeneous object. The Code is as follows:
1 # include <iostream> 2 using namespace std; 3 // class B; 4 class B {5 private: 6 int k; 7 public: 8 B (): k (90) {} 9}; 10 class A {11 public: 12 int I; 13 A (): I (10), j (20) {} 14 void print (A * p) {15 p-> j = 50; 16 cout <"p-> j =" <p-> j <endl; 17} 18 void print (B * p) {19 p-> k = 1; 20 cout <"p-> k =" <p-> k <endl; 21} 22 private: 23 int j; 24}; 25 26 int main () 27 {28 A; 29 a B; 30 B c; 31 //. print (& B); // you can use the member function to access the private member variables of the same class. In this way, you can modify the value of the private variable that the corresponding object points to 32. print (& c); // different classes cannot access 33}
View Code
The above Code has the following error during compilation:
[Error] 'int B: k' is private
Supplement:
It also needs to be added that this is only valid during compilation. It is also possible to modify private member variables at runtime.
For example, to forcibly convert the pointer of a class object into the following form:
A;
Int * p = & a; // here, a is the object of Class.
* P = 20; // modify the value at the address to modify the member variables of the class.
Condition: (p points to the position of the private member variable in the class, because a class occupies a piece of memory and all member variables are stored in order, so as long as you get the base pointer of the Class Object and know the offset of the private member variable, you can modify the private value at runtime)
You can modify the value of the private member variable in the method above.
Description: The OOP idea of C ++ only exists in the source code (before compilation is successful). The compiled code does not have the OOP idea.
(That is to say, the compiler acts as a rule to limit the OOP of the code, but the. o file after compilation is the same as the. o file after C compilation, all of which become procedural)
So we can modify the content pointed to by the pointer as long as we can get the pointer at runtime.