1: private, public, protected member functions
PRIVATE: It can only be accessed by 1. functions in this class, 2. Other functions.
Cannot be accessed by any other user, nor can the object of this class be accessed.
Protected: it can be accessed by 1. functions in this class, 2. Sub-class functions, and 3. Its membership functions.
But cannot be accessed by objects of this class.
Public: it can be accessed by 1. functions in this class, 2. Sub-class functions, 3. Its friends functions, or 4. objects in this class.
Note: There are three types of functions: normal non-member functions set as friends, member functions of other classes set as friends, and all member functions in the friends class.
2: private, public, and protected class inheritance
Private attributes cannot be inherited.
Using private inheritance, the protected and public attributes of the parent class are changed to private in the subclass;
Using Protected inheritance, the protected and public attributes of the parent class are changed to protected in the subclass;
Use public inheritance. The protected and public attributes in the parent class are not changed;
As follows:
Public: protected: Private:
Public inherits from public protected and is unavailable.
Protected inherits protected. Protected is unavailable.
Private inherits Private unavailability
Protected inheritance and private inheritance can reduce access permissions.
1 # include <stdio. h>
2
3 class base
4 {
5 public:
6 void fn_pub ()
7 {
8 };
9 private:
10 void fn_priv ()
11 {
12 };
13 protected:
14 void fn_prot ()
15 {
16 };
17 };
18
19 class derived_pub: public Base
20 {
21 public:
22 void test ()
23 {
24 fn_pub ();
25 // fn_priv (); // fail !!
26 fn_prot ();
27 };
28 };
29
30 class derived_priv: Private Base
31 {
32 public:
33 void test ()
34 {
35 fn_pub ();
36 // fn_priv (); // fail !!
37 fn_prot ();
38 };
39 };
40
41 class derived_protec: protected base
42 {
43 public:
44 void test ()
45 {
46 fn_pub ();
47 // fn_priv (); // fail !!
48 fn_prot ();
49 };
50 };
51
52
53derived_pub drvd_pub;
54derived_priv drv_priv;
55derived_protec drv_protec;
56
57 void main ()
58 {
59 drvd_pub.fn_pub ();
60 // drvd_pub.fn_priv (); // fail !!
61 // drvd_pub.fn_prot (); // fail !!
62
63 // drv_priv.fn_pub (); // fail !!
64 // drv_priv.fn_priv (); // fail !!
65 // drv_priv.fn_prot (); // fail !!
66
67 // drv_protec.fn_pub (); // fail !!
68 // drv_protec.fn_priv (); // fail !!
69 // drv_protec.fn_prot (); // fail !!
70
71}
Refer:
Http://blog.minidx.com/2007/11/03/48.html