15. public/protected/private

类成员访问权限(可访问: \(\checkmark\)

权限

类成员

类对象

派生类成员

友元函数

public

\(\checkmark\)

\(\checkmark\)

\(\checkmark\)

\(\checkmark\)

private

\(\checkmark\)

\(\times\)

\(\times\)

\(\checkmark\)

protected

\(\checkmark\)

\(\times\)

\(\checkmark\)

\(\checkmark\)

15.1. 继承

public继承下对基类成员的访问权限

内部权限

权限变化(相对于派生类)

派生类成员

派生类对象

public

-> public

\(\checkmark\)

\(\checkmark\)

private

-> private

\(\times\)

\(\times\)

protected

-> protected

\(\checkmark\)

\(\times\)

private继承下对基类成员的访问权限

内部权限

权限变化(相对于派生类)

派生类成员

派生类对象

public

-> private

\(\checkmark\)

\(\times\)

private

-> private

\(\times\)

\(\times\)

protected

-> private

\(\checkmark\)

\(\times\)

protected继承下对基类成员的访问权限

内部权限

权限变化(相对于派生类)

派生类成员

派生类对象

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. 参考资料

  1. C++中关于public、protect、private的访问权限控制

  1. C++的关键字public,private和protected

  1. C++中public,protected,private访问