15. public/protected/private
权限 |
类成员 |
类对象 |
派生类成员 |
友元函数 |
---|---|---|---|---|
public |
\(\checkmark\) |
\(\checkmark\) |
\(\checkmark\) |
\(\checkmark\) |
private |
\(\checkmark\) |
\(\times\) |
\(\times\) |
\(\checkmark\) |
protected |
\(\checkmark\) |
\(\times\) |
\(\checkmark\) |
\(\checkmark\) |
15.1. 继承
内部权限 |
权限变化(相对于派生类) |
派生类成员 |
派生类对象 |
---|---|---|---|
public |
-> public |
\(\checkmark\) |
\(\checkmark\) |
private |
-> private |
\(\times\) |
\(\times\) |
protected |
-> protected |
\(\checkmark\) |
\(\times\) |
内部权限 |
权限变化(相对于派生类) |
派生类成员 |
派生类对象 |
---|---|---|---|
public |
-> private |
\(\checkmark\) |
\(\times\) |
private |
-> private |
\(\times\) |
\(\times\) |
protected |
-> private |
\(\checkmark\) |
\(\times\) |
内部权限 |
权限变化(相对于派生类) |
派生类成员 |
派生类对象 |
---|---|---|---|
public |
-> protected |
\(\checkmark\) |
\(\times\) |
private |
-> private |
\(\times\) |
\(\times\) |
protected |
-> protected |
\(\checkmark\) |
\(\times\) |
15.2. class 与 struct
class 不写权限修饰符,成员默认是
private
;struct 的成员默认是public
。class 的继承默认是
private
,struct 的继承默认是public
。
15.3. 参考资料
C++中关于public、protect、private的访问权限控制
C++的关键字public,private和protected
C++中public,protected,private访问