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

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄