私人成员派生类

来源:互联网 发布:网络播放量排行榜2016 编辑:程序博客网 时间:2024/06/10 01:17

前面的课程我们遗传的我们已经让所有的公共数据成员为了简化的例子在这一节我们将谈论遗传过程访问说明符的作用,以及ascover不同类型遗传可能在C++

这一点你见过的私人和公共接入说明符确定谁能访问类的成员。有快速的环境公众成员可以上网的人私有成员只能同一个类的成员函数访问。注意这意味着不能访问私人成员派生类


1234567class 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};

当处理继承类事情有点复杂

第一,有三分之一的访问说明符,我们还谈论因为它是唯一有用的遗传背景。受保护的访问说明符限制访问同一个类的成员函数派生类

12345678910111213141516171819202122232425262728293031class Base{public:    int m_nPublic; // can be accessed by anybodyprivate:    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}



0 0
原创粉丝点击