Public Private Protect Inheritance and access specifiers
In the previous lessons on inheritance, we’ve been making all of our data members public in order to simplify the examples. In this section, we’ll talk about the role of access specifiers in the inheritance process, as well as cover the different types of
inheritance possible in C++.
To this point, you’ve seen the private and public access specifiers, which determine who can access the members of a class. As a quick refresher, public members can be accessed by anybody. Private members can only be accessed by member functions of the same
class. Note that this means derived classes can not access private members!
|
1
2
3
4
5
6
7
|
classBase{private: intm_nPrivate; // can only be accessed by Base member functions (not derived classes)public: intm_nPublic; // can be accessed by anybody}; |
When dealing with inherited classes, things get a bit more complex.
First, there is a third access specifier that we have yet to talk about because it’s only useful in an inheritance context. The
protected access specifier restricts access to member functions of the same class, or those of derived classes.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
classBase{public: intm_nPublic; // can be accessed by anybodyprivate: intm_nPrivate; // can only be accessed by Base member functions (but not derived classes)protected: intm_nProtected; // can be accessed by Base member functions, or derived classes.};classDerived: publicBase{public: Derived() { // Derived's access to Base members is not influenced by the type of inheritance used, // so the following is always true: m_nPublic = 1;// allowed: can access public base members from derived class m_nPrivate = 2;// not allowed: can not access private base members from derived class m_nProtected = 3;// allowed: can access protected base members from derived class }};int { Base cBase; cBase.m_nPublic = 1;// allowed: can access public members from outside class cBase.m_nPrivate = 2;// not allowed: can not access private members from outside class cBase.m_nProtected = 3;// not allowed: can not access protected members from outside class} |
Second, when a derived class inherits from a base class, the access specifiers may change depending on the method of inheritance. There are three different ways for classes to inherit from other classes: public, private, and protected.
To do so, simply specify which type of access you want when choosing the class to inherit from:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// Inherit from Base publiclyclassPub: public {};// Inherit from Base privatelyclassPri: privateBase{};// Inherit from Base protectedlyclassPro: protectedBase{};classDef: Base // Defaults to private inheritance{}; |
If you do not choose an inheritance type, C++ defaults to private inheritance (just like members default to private access if you do not specify otherwise).
That gives us 9 combinations: 3 member access specifiers (public, private, and protected), and 3 inheritance types (public, private, and protected).
The rest of this section will be devoted to explaining the difference between these.
Before we get started, the following should be kept in mind as we step through the examples. There are three ways that members can be accessed:
- A class can always access it’s own members regardless of access specifier.
- The public accesses the members of a class based on the access specifiers of that class.
- A derived class accesses inherited members based on the access specifiers of its immediate parent. A derived class can always access it’s own members regardless of access specifier.
This may be a little confusing at first, but hopefully will become clearer as we step through the examples.
Public inheritance
Public inheritance is by far the most commonly used type of inheritance. In fact, very rarely will you use the other types of inheritance, so your primary focus should be on understanding this section. Fortunately, public inheritance is also the easiest
to understand. When you inherit a base class publicly, all members keep their original access specifications. Private members stay private, protected members stay protected, and public members stay public.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
classBase{public: intm_nPublic;private: intm_nPrivate;protected: intm_nProtected;};classPub: public { // Public inheritance means: // m_nPublic stays public // m_nPrivate stays private // m_nProtected stays protected Pub() { // The derived class always uses the immediate parent's class access specifications // Thus, Pub uses Base's access specifiers m_nPublic = 1;// okay: anybody can access public members m_nPrivate = 2;// not okay: derived classes can't access private members in the base class! m_nProtected = 3;// okay: derived classes can access protected members }};int { // Outside access uses the access specifiers of the class being accessed. // In this case, the access specifiers of cPub. Because Pub has inherited publicly from Base, // no access specifiers have been changed. Pub cPub; cPub.m_nPublic = 1;// okay: anybody can access public members cPub.m_nPrivate = 2;// not okay: can not access private members from outside class cPub.m_nProtected = 3;// not okay: can not access protected members from outside class} |
This is fairly straightforward. The things worth noting are:
- Derived classes can not directly access private members of the base class.
- The protected access specifier allows derived classes to directly access members of the base class while not exposing those members to the public.
- The derived class uses access specifiers from the base class.
- The outside uses access specifiers from the derived class.
To summarize in table form:
| Public inheritance | |||
|---|---|---|---|
| Base access specifier | Derived access specifier | Derived class access? | Public access? |
| Public | Public | Yes | Yes |
| Private | Private | No | No |
| Protected | Protected | Yes | No |
Private inheritance
With private inheritance, all members from the base class are inherited as private. This means private members stay private, and protected and public members become private.
Note that this does not affect that way that the derived class accesses members inherited from its parent! It only affects the code trying to access those members through the derived class.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
classBase{public: intm_nPublic;private: intm_nPrivate;protected: intm_nProtected;};classPri: privateBase{ // Private inheritance means: // m_nPublic becomes private // m_nPrivate stays private // m_nProtected becomes private Pri() { // The derived class always uses the immediate parent's class access specifications // Thus, Pub uses Base's access specifiers m_nPublic = 1;// okay: anybody can access public members m_nPrivate = 2;// not okay: derived classes can't access private members in the base class! m_nProtected = 3;// okay: derived classes can access protected members }};int { // Outside access uses the access specifiers of the class being accessed. // Note that because Pri has inherited privately from Base, // all members of Base have become private when access through Pri. Pri cPri; cPri.m_nPublic = 1;// not okay: m_nPublic is now a private member when accessed through Pri cPri.m_nPrivate = 2;// not okay: can not access private members from outside class cPri.m_nProtected = 3;// not okay: m_nProtected is now a private member when accessed through Pri // However, we can still access Base members as normal through Base: Base cBase; cBase.m_nPublic = 1;// okay, m_nPublic is public cBase.m_nPrivate = 2;// not okay, m_nPrivate is private cBase.m_nProtected = 3;// not okay, m_nProtected is protected} |
To summarize in table form:
| Private inheritance | |||
|---|---|---|---|
| Base access specifier | Derived access specifier | Derived class access? | Public access? |
| Public | Private | Yes | No |
| Private | Private | No | No |
| Protected | Private | Yes | No |
Protected inheritance
Protected inheritance is the last method of inheritance. It is almost never used, except in very particular cases. With protected inheritance, the public and protected members become protected, and private members stay private.
To summarize in table form:
| Protected inheritance | |||
|---|---|---|---|
| Base access specifier | Derived access specifier | Derived class access? | Public access? |
| Public | Protected | Yes | No |
| Private | Private | No | No |
| Protected | Protected | Yes | No |
Protected inheritance is similar to private inheritance. However, classes derived from the derived class still have access to the public and protected members directly. The public (stuff outside the class) does not.
Summary
The way that the access specifiers, inheritance types, and derived classes interact causes a lot of confusion. To try and clarify things as much as possible:
First, the base class sets it’s access specifiers. The base class can always access it’s own members. The access specifiers only affect whether outsiders and derived classes can access those members.
Second, derived classes have access to base class members based on the access specifiers of the immediate parent. The way a derived class accesses inherited members is not affected by the inheritance method used!
Finally, derived classes can change the access type of inherited members based on the inheritance method used. This does not affect the derived classes own members, which have their own access specifiers. It only affects whether outsiders and classes derived
from the derived class can access those inherited members.
A final example:
|
1
2
3
4
5
6
7
8
9
|
classBase{public: intm_nPublic;private: intm_nPrivate;protected: intm_nProtected;}; |
Base can access it’s own members without restriction. The public can only access m_nPublic. Derived classes can access m_nPublic and m_nProtected.
|
1
2
3
4
5
6
7
8
9
|
classD2: private {public: intm_nPublic2;private: intm_nPrivate2;protected: intm_nProtected2;} |
D2 can access it’s own members without restriction. D2 can access Base’s members based on Base’s access specifiers. Thus, it can access m_nPublic and m_nProtected, but not m_nPrivate. Because D2 inherited Base privately, m_nPublic, m_nPrivate, and m_nProtected
are now private when accessed through D2. This means the public can not access any of these variables when using a D2 object, nor can any classes derived from D2.
|
1
2
3
4
5
6
7
8
9
|
classD3: public {public: intm_nPublic3;private: intm_nPrivate3;protected: intm_nProtected3;}; |
D3 can access it’s own members without restriction. D3 can access D2′s members based on D2′s access specifiers. Thus, D3 has access to m_nPublic2 and m_nProtected2, but not m_nPrivate2. D3 access to Base members is controlled by the access specifier of it’s
immediate parent. This means D3 does not have access to any of Base’s members because they all became private when D2 inherited them.
http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
Public Private Protect Inheritance and access specifiers的更多相关文章
- public private protect
public 公有 使用public意味着声明public之后的成员对每个人都是可用的 private 私有 除非必须公开底层实现细目,否则就应该将所有的域指定为private protect 继 ...
- public private, protect. 以及继承。 草稿。
#include <iostream>#include <thread>#include <memory> // | 父类的public成员 | 父类的protec ...
- C++ public private protect 继承关系(链接)
基础链接 总结: public继承基类成员访问权限没有变化; protected继承基类public和protected权限变为protected,基类private不变. private继承基类p ...
- public、protect、private在父类子类中使用
先贴出一张,直观的.估计大家都见过的关于public.protect.private的范围图 作用域 当前类 同一package 子孙类 其他package public T ...
- c/c++再学习:C++中public、protect、private的访问权限控制
C++中public.protect.private的访问权限控制 访问权限 一个类的public成员变量.成员函数,可以通过类的成员函数.类的实例变量进行访问 一个类的protected成员变量.成 ...
- c++三种继承方式public,protect,private
C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...
- c++ 之private /protect/ public
C++中public.protect.private的访问权限控制 访问权限 一个类的public成员变量.成员函数,可以通过类的成员函数.类的实例变量进行访问 一个类的protected成员变量.成 ...
- 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)
访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...
- public private protected和默认的区别(转自百度)
public private protected和默认的区别 Java中对类以及类中的成员变量和成员方法通过访问控制符(access specifier)进行区分控制.刚学Java语言的同学可能对pu ...
随机推荐
- python3实践-从网站获取数据(Carbon Market Data-BJ) (pandas,bs4)
自己边看边实践一些简单的实际应用,下面的程序是从某个网站上获取需要的数据. 在编写的过程中,通过学习陆续了解到一些方法,发现Python真的是很便捷. 尤其是用pandas获取网页中的表格数据,真的是 ...
- 使用NGUI来制作技能的CD冷却效果
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CDScript ...
- 初识C#设计模式
利用设计模式可以使我们的代码更灵活,更容易扩展,更容易维护.各种面向对象的程序设计语言都提供了基本相同的机制:比如类.继承.派生.多态等等.但是又有各自的特色,C# 中的反射机制便是一个很重要的工具, ...
- Android apk去广告
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 下载地址: [北方网通] [电信网通] [下载说明] 1 点击上面的地址,打开下载页面 ...
- 【UOJ #171】【WC 2016】挑战NPC
http://uoj.ac/problem/171 带花树开花时的u和v一定要记清楚顺序,想好了再写,数据范围也要算好! 对每个筐子拆成3个点,连成一个三元环,对每个(u,v),让u和v的3个点都连边 ...
- BZOJ 2631 tree(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2631 [题目大意] 要求支持链修改,链查询,边断开,连接操作 [题解] 链修改分乘和加 ...
- BZOJ 1982 [Spoj 2021]Moving Pebbles(博弈论)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1982 [题目大意] 两个人玩游戏. 每次任选一堆,首先拿掉至少一个石头, 然后移动任意 ...
- 【贪心】【堆】Gym - 101128C - Canvas Painting
一些画布,每块有其大小,一开始都是白的,你任意将它们排序,然后一次操作可以选择一段连续的相同颜色的画布,从中任选一个位置,左侧涂上任意一种颜色,右侧涂上另一种.消耗是这一段画布的总的大小.问你要将所有 ...
- 【强联通分量缩点】【Tarjan】bzoj1051 [HAOI2006]受欢迎的牛
就是看是否有一些点,从其他任何点出发都可到达 定理:有向无环图中唯一出度为0的点,一定可以由任何点出发均可达. 所以缩点,若出度为零的点(强联通分量)唯一,则答案为该强联通分量中点的度数. 若不唯一, ...
- 小H的硬币游戏
题目大意: 有n个物品排成一排,每个物品都有自己的价值,你每次可以从中挑选两个距离为k的物品取走,问最大的收益. (如果原来两个物品中间有物品被取走,距离不变) 思路: 贪心. 先按照每个物品的位置m ...