class Per {
  public:
    Per() = default;
    int pubPer = 1;
  protected:
    int protPer = 1;
  private:
    int prvPer = 1;
};
class Stu: protected Per {
  public:
    Stu() = default;
    int pubStu = 1;
  protected:
    int protStu = 1;
  private:
    int priStu = 1;
};
class Monitor: public Stu {
  public:
    Monitor() = default;
    int pubMonitor = 1;
  protected:
    int protMonitor = 1;
  private:
    int priMonitor = 1;
};
int main () {
  Stu s;
  Monitor m;
  s.pubPer++; // error line
  s.protPer++; // error line
  m.pubPer++; // error line
  m.protPer++; // error line
  return 0;
}
当 保持Monitor公有继承于Stu 时:
若 Stu 保护继承于 Per, 则 Stu中对 Per中的公有成员访问权限被重置为protected,即 Stu 的实例不能再访问 Per 中的公有成员, 也就是Stu实例无法访问Per中的任何成员,Monitot实例也一样无法访问。 同理,若Stu 私有继承于 Per 也能实现相似的限制效果。即阻断Stu类向下(包括Stu)的所有类的实例对基类所有成员的访问。
class Per {
  public:
    Per() = default;
    int pubPer = 1;
  protected:
    int protPer = 1;
  private:
    int prvPer = 1;
};
class Stu: private Per {
  public:
    Stu() = default;
    int pubStu = 1;
  protected:
    int protStu = 1;
  private:
    int priStu = 1;
};
class Monitor: public Stu {
  public:
    Monitor() = default;
    int pubMonitor = 1;
    void watch () {
      pubPer++;  // error line
    }
  protected:
    int protMonitor = 1;
  private:
    int priMonitor = 1;
};
int main () {
  Stu s;
  Monitor m;
  s.pubPer++; // error line
  s.protPer++; // error line
  m.pubPer++; // error line
  m.protPer++; // error line
  return 0;
}
class Per {
  public:
    Per() = default;
};
class Stu: protected Per {
  public:
    Stu() = default;
};
int main () {
  Stu s;
  Per p = s; // error line
  return 0;
}
即对某个特定作用域来说,如若基类中的公有成员可访问,则能够实现派生类类型到基类类型的转换。
class Per {
  public:
    Per() = default;
    void eat () {}
};
class Stu: private Per {
  public:
    Stu() = default;
    void test () {
      Per p = *this;
    }
  private:
    Stu *thisS;
};
class Monitor: public Stu {
  public:
    void test () {
      Stu s;
      Per p = s; // error line
    }
};
int main () {
  Stu s;
  Per p = s; // error line
  return 0;
}
通过派生访问说明符与成员访问说明符的配合,可以更加全面地实现各种应用场景。
回顾C++:访问权限控制之 公有、保护、私有继承用法详解与总结
原文:https://www.cnblogs.com/pomelott/p/13897832.html