私人成员派生类
2014-10-27 14:20
10 查看
前面的课程我们遗传的,我们已经让所有的公共数据成员为了简化的例子。在这一节,我们将谈论在遗传过程访问说明符的作用,以及ascover的不同类型遗传可能在C++。
这一点,你见过的私人和公共接入说明符,它确定谁能访问类的成员。有快速的环境,公众成员可以上网的人。私有成员只能由同一个类的成员函数访问。注意,这意味着不能访问私人成员派生类。
当处理继承类,事情有点复杂。
第一,有三分之一的访问说明符,我们还谈论因为它是唯一有用的遗传背景。受保护的访问说明符限制访问同一个类的成员函数,或派生类。
这一点,你见过的私人和公共接入说明符,它确定谁能访问类的成员。有快速的环境,公众成员可以上网的人。私有成员只能由同一个类的成员函数访问。注意,这意味着不能访问私人成员派生类。
1 2 3 4 5 6 7 class Base { private: int m_nPrivate; // can only be accessed by Base member functions (not derived classes) public: int m_nPublic; // can be accessed by anybody };
当处理继承类,事情有点复杂。
第一,有三分之一的访问说明符,我们还谈论因为它是唯一有用的遗传背景。受保护的访问说明符限制访问同一个类的成员函数,或派生类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class Base { public: int m_nPublic; // can be accessed by anybody private: int m_nPrivate; // can only be accessed by Base member functions (but not derived classes) protected: int m_nProtected; // can be accessed by Base member functions, or derived classes. }; class Derived: public Base { public: Derived() { // Derived's access to Base members is not influenced by the type of inheritance used, // so the following is always true: m_nPublic = 1; // allowed: can access public base members from derived class m_nPrivate = 2; // not allowed: can not access private base members from derived class m_nProtected = 3; // allowed: can access protected base members from derived class } }; int main() { Base cBase; cBase.m_nPublic = 1; // allowed: can access public members from outside class cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class }
相关文章推荐
- C++语言基础 例程 派生类成员的访问属性
- C++基类和派生类私有、保护、公有成员访问控制以及动态绑定、三种继承
- 例4.8 含有对象成员的派生类构造函数和析构函数的执行顺序
- C++继承具体解释之二——派生类成员函数具体解释(函数隐藏、构造函数与兼容覆盖规则)
- 指向基类/派生类指针,指向基类成员/派生类成员指针,及互相赋值
- 关于继承,不同继承方式,对于派生类成员以及基类的可见性
- 第十三周—【项目1】理解基类中成员的访问限定符和派生类的继承方式
- C++编程入门系列之三十七(继承与派生:派生类对基类成员的访问控制之公有继承)
- YTU-OJ-Problem J: B3 指向基类的指针访问派生类的成员函数
- 派生类内只能定义派生类新增加的成员而不能定义基类成员
- 第十二周实验报告(任务一)【派生类成员的访问属性的原则】
- 解析C++中派生的概念以及派生类成员的访问属性
- 关于继承,不同继承方式,对于派生类成员以及基类的可见性
- 关于派生类拷贝构造函数中使用基类私有类对象成员的问题
- 派生类的对象只能访问基类的public成员
- 基类、派生类、派生类成员变量的构造和析构顺序
- 例4.9 含有多个对象成员的派生类构造函数的执行顺序
- C++ 中基类及派生类中继承成员访问权限
- C++如何在派生类成员函数内调用基类的成员函数(包括构造函数)
- 派生类在构造对象时,先构造基类对象,再构造派生类自身的成员