我在 http://www.geeksforgeeks.org/inheritance-in-c/ 中查看继承概念.我对作者写的几句话感到困惑。作者在一处说
If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class .......
这意味着我们在 C++ 中有类似公共(public)类的东西吗?文章下表还表明存在公共(public)/ protected 类的概念。
我查看了其他一些 SO 帖子 ( Use of "Public" in a derived class declaration? ),没有发现对 Public、Private 或 protected 类本身的引用。本帖https://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-c公开的谈话,但通过头文件。
最佳答案
Public
、Protected
和Private
关键字是C++
中的可见性标签。 c++
中没有public
、protected
和private
类类型(如Java
).这三个关键字也用在完全不同的上下文中来指定可见性继承模型。
下表列出了组件声明和继承模型的所有可能组合,展示了当子类被完全定义时对组件的访问结果。
它是这样写的(看第一行):
if a component is declared as public and its class is inherited as public the resulting access is public.
请看下面的示例:
class Super {
private: int x;
protected: int y;
public: int z;
};
class Sub : protected Super {};
在类 Sub 中对变量 y、z 的最终访问是 protected ,而对变量 x 是无访问的。
注意:
缺少修饰符会产生 private
。
关于c++ - C++中类名的访问修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46411484/