You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/member-access-control-cpp.md
+40-23Lines changed: 40 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -68,28 +68,44 @@ protected: // Declare protected function for derived classes only.
68
68
69
69
```cpp
70
70
// access_specifiers_for_base_classes.cpp
71
-
classBaseClass
72
-
{
73
-
public:
74
-
int PublicFunc(); // Declare a public member.
75
-
protected:
76
-
int ProtectedFunc(); // Declare a protected member.
77
-
private:
78
-
int PrivateFunc(); // Declare a private member.
79
-
};
80
-
71
+
classBaseClass
72
+
{
73
+
public:
74
+
int PublicFunc(); // Declare a public member.
75
+
protected:
76
+
int ProtectedFunc(); // Declare a protected member.
77
+
private:
78
+
int PrivateFunc(); // Declare a private member.
79
+
};
80
+
81
81
// Declare two classes derived from BaseClass.
82
-
classDerivedClass1 : publicBaseClass
83
-
{
84
-
};
85
-
86
-
classDerivedClass2 : privateBaseClass
87
-
{
88
-
};
89
-
90
-
intmain()
91
-
{
92
-
}
82
+
classDerivedClass1 : publicBaseClass
83
+
{
84
+
void foo()
85
+
{
86
+
PublicFunc();
87
+
ProtectedFunc();
88
+
PrivateFunc(); // function is inaccessible
89
+
}
90
+
};
91
+
92
+
classDerivedClass2 : privateBaseClass
93
+
{
94
+
void foo()
95
+
{
96
+
PublicFunc();
97
+
ProtectedFunc();
98
+
PrivateFunc(); // function is inaccessible
99
+
}
100
+
};
101
+
102
+
intmain()
103
+
{
104
+
DerivedClass1 derived_class1;
105
+
DerivedClass2 derived_class2;
106
+
derived_class1.PublicFunc();
107
+
derived_class2.PublicFunc(); // function is inaccessible
108
+
}
93
109
```
94
110
95
111
In `DerivedClass1`, the member function `PublicFunc` is a public member and `ProtectedFunc` is a protected member because `BaseClass` is a public base class. `PrivateFunc` is private to `BaseClass`, and it is inaccessible to any derived classes.
@@ -216,5 +232,6 @@ Access Along Paths of an Inheritance Graph
216
232
217
233
In the figure, a name declared in class `VBase` is always reached through class `RightPath`. The right path is more accessible because `RightPath` declares `VBase` as a public base class, whereas `LeftPath` declares `VBase` as private.
218
234
219
-
## See also
220
-
[C++ Language Reference](../cpp/cpp-language-reference.md)
235
+
236
+
## See Also
237
+
[C++ Language Reference](../cpp/cpp-language-reference.md)
0 commit comments