您的位置:首页 > 编程语言 > C语言/C++

C++之Access Label, Inheritance, and Friend

2012-09-28 04:07 615 查看


************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:
Like private members, protected members are inaccessible to users of the class.
Like public members, protected members are accessible to classes derived from the class.
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息