类的访问级别

类中三种访问级别关键字: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;
}

 

posted @ 2019-05-10 15:40  张不源  Views(498)  Comments(0Edit  收藏  举报