继承的访问级别

类中三种访问级别关键字:public,private ,protect

通过protect关键字确定访问级别。

  1. 修饰的成员可以被子类访问

  2. 修饰的成员不可被外界直接访问

Class Parent 
{
protected:                 // 定义父类的变量为 protected
    int parent_properity;
public:
    void parent_function(){};
};

Class Child : public Parent 
{
    int child_properity;     
public:
    void child_function(){ parent_properity = 1;};  //在子类的内部可以访问父类的私有成员
};

int main(void)
{
    Child c;
    //c.parent_properity = 100;  // 在外部不可以访问父类私有成员
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zsy12138/p/10844763.html
今日推荐