派生类的对象只能访问基类的public成员

  派生类中的成员可以访问基类的public成员和protected成员,但不能访问基类的private成员。

  派生类的对象只能访问基类的public成员。

  protected(保护继承),private(私有继承)

  派生类中的成员可以访问基类的public成员和protected成员,但不能访问基类的private成员。

  派生类的对象不能访问基类的任何成员。

  example 1:

  #include

  class A

  {

  public:

  void fun1(int a) {cout<

  void fun2(int b) {cout<

  };

  class B:public A

  {

  public:

  void fun3() {cout<<"It is in class B."<

  };

  int main()

  {

  B b;

  A a;

  b.fun3(); //Y(正确)

  b.fun2(); //Y

  b.fun1(); //Y

  a.fun3(); //N(错误)

  a.fun2(); //Y

  a.fun1(); //Y

  }

  example2:

  #include

  class A

  {

  public:

  void f1();

  A() {i1 = 10; j1 = 11;}

  protected:

  int j1;

  private:

  int i1;

  };

  class B:public A

  {

  public:

  void f2();

  B() {i2 = 20; j2 = 21;}

  protected:

  int j2;

  private:

  int i2;

  };

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值