C++之Access Label, Inheritance, and Friend

************Inheritance************

Three modes of inheritance in C++: public, protected, private. For example: class D: public A, protected B, private C. (A, B, C, D are all class types)

class B: public (protected, private) A

In all modes of inheritance, the private members of base class A remain PRIVATE to A and are NOT accessible from members of B.

When B inherits publically from A, the protected members of A become protected members of B, and the public members of A become public members of B.

When the inheritance mode is protected, both the public and protected members of A become protected members of B.

If the derivation mode is private, then the public and protected members of A become private in B.

************friend************

The friend mechanism allows a class to grant access to its nonpublic members  to specified functions or classes.

Friendship is not inherited.

************access control************

public members: The public section of a class defines members that can be accessed from any part of the program.

private members: Codes that is NOT part of the class does not have access to the private members.

protected members:

  1. Like private members, protected members are inaccessible to users of the class.
  2. Like public members, protected members are accessible to classes derived from the class.
  3. A derived object may access the protected members of its base class ONLY through a derived object. The derived class has no special access to the protected members of base type objects.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值