环境:QT 5.12

继承方式规定了子类如何访问从基类继承的成员。继承方式有public、protected、private三种。继承方式不影响派生类的访问权限,影响了从基类继承而来的成员的访问权限,包括派生类内的访问权限和派生类对象的访问权限。在派生类内,对于从基类继承下来的数据成员而言,就有四种情况了,分别是public、protected、private、invisible(不可见)。

以下列出结论,然后使用代码进行验证。

1.protected继承,基类中的public成员和protected成员在派生类中均为protected成员,基类中的private成员在派生类中为invisiable,无法访问。多次protected继承后,基类中public成员和protected成员在孙子类中仍然为protected成员。

1) 初始代码如下

类A中有public、protected、private三种属性的成员变量,类B继承类A,继承方式为protected,类B中也有public、protected、private三种属性的成员变量。

 1 #include <iostream>
2
3 using namespace std;
4
5 class A
6 {
7 public:
8 A(int three)
9 :a_three(three){}
10
11 int a_three;
12 protected:
13 int a_two = 20;
14 private:
15 int a_one = 30;
16 };
17
18 class B: protected A
19 {
20 public:
21 B(int three_1, int three_2)
22 :A(three_1), b_three(three_2){}
23
24 int b_three;
25
26 void display()
27 {
28 cout<<"A::a_three: "<<a_three<<endl;
29 cout<<"A::a_two : "<<a_two<<endl;
30 }
31 protected:
32 int b_two= 50;
33 private:
34 int b_one = 60;
35 };
36
37 int main()
38 {
39 B bb(10, 30);
40 bb.display();
41
42 return 0;
43 }

运行结果

派生类B中public函数display(),可以访问从基类继承而来的public成员和protected成员,因为这两个成员在派生类中作为派生类的protected成员了。

2) 再添加上孙子类,类C继承类B,继承方式为protected。

 1 class C: protected B
2 {
3 public:
4 C(int three_1, int three_2, int three_3)
5 :B(three_1, three_2), c_three(three_3){}
6
7 int c_three;
8
9 void show()
10 {
11 cout<<"A::a_three: "<<a_three<<endl;
12 cout<<"A::a_two : "<<a_two<<endl;
13 cout<<"A::b_three: "<<b_three<<endl;
14 cout<<"A::b_two : "<<b_two<<endl;
15 }
16 protected:
17 int c_two = 80;
18 private:
19 int c_one = 90;
20 };

在孙子类C中public函数show()内打印祖父类A中的public和protected数据成员。main函数也进行相应的调整,调用类C中show()函数。

 1 int main()
2 {
3 B bb(10, 30);
4 bb.display();
5
6 cout<<"----------"<<endl;
7 C cc(10, 30, 70);
8 cc.show();
9 //cout<<cc.a_three<<endl;
10 //cout<<cc.a_two<<endl;
11 //cout<<cc.b_three<<endl;
12 //cout<<cc.b_three<<endl;
13
14 return 0;
15 }

运行结果

在孙子类C中,仍然可以访问祖父类A中的public和protected成员a_three、a_two(还有父类B中的public、protected成员b_three、b_two),符合结论1。孙子类的对象cc不能直接访问这些成员,也证实了从基类继承下来的public和protected成员,到了派生类中确是protected属性。

2.private继承,基类中的public成员和protected成员在派生类中均为private成员,基类中的private成员在派生类中为invisible,无法访问。多次private继承后,最初的基类中的成员在孙子类中均为invisible,无法访问

当我们将上面代码中类B的继承方式和类C的继承方式由protected修改为private时

第60、61两行代码没有报错,在类B的成员函数display()中,它是可以访问基类A中的public、protected成员a_three、a_two。

第47、48两行代码报错,在孙子类C的成员函数show()中,提示它不可以访问祖父类A中的public、protected成员a_three、a_two。

说明这两个数据成员a_three、a_two在类C的父类B中,是类B的private成员,private成员在派生类中是invisible。第60、61两行代码,类内部可以访问自己的private数据成员(private继承后,父类A的public和proteced成员在派生类B中成为派生类B的private成员),符合预期。

参考资料:

《C++基础与提高》 王桂林

protected和private继承方式的不同的更多相关文章

  1. C++ public、protected、private 继承方式的区别

    访问修饰符 public.protected.private,无论是修饰类内成员(变量.函数),还是修饰继承方式,本质上实现的都是可见性的控制. Difference between private, ...

  2. c++三种继承方式public,protect,private

    C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...

  3. C++中的三种继承public,protected,private

    ( c++默认class是private继承且class内的成员默认都是private struct 默认位public 继承,struct内成员默认是public  ) 三种访问权限 public: ...

  4. C++学习15 继承权限和继承方式

    C++继承的一般语法为: class 派生类名:[继承方式] 基类名{ 派生类新增加的成员 }; 继承方式限定了基类成员在派生类中的访问权限,包括 public(公有的).private(私有的)和 ...

  5. 3.2 C++继承方式

    参考: http://www.weixueyuan.net/view/6359.html  总结: 子类继承父类,继承方式将限制父类的成员属性在子类中的访问权限,子类访问父类的成员,也需要遵循其成员的 ...

  6. mfc 类三种继承方式下的访问

    知识点 public private protected 三种继承方式 三种继承方式的区别 public 关键字意味着在其后声明的所有成员及对象都可以访问. private 关键字意味着除了该类型的创 ...

  7. C++中public、protected、private的差别

    第一: private,public,protected的訪问范围:   private: 仅仅能由该类中的函数.其友元函数訪问,不能被不论什么其它訪问.该类的对象也不能訪问. protected: ...

  8. C++ 类中的3种访问权限和继承方式

    访问权限:public 可以被任意实体访问,protected 只允许子类(无论什么继承方式)及本类的成员函数访问,private 只允许本类的成员函数访问.三种继承方式分别是 public 继承,p ...

  9. [c++] C++中public、protected、private的区别

    转:https://blog.csdn.net/vanturman/article/details/79393317 第一: private,public,protected的访问范围: privat ...

  10. C++中的三种继承方式

    1,被忽略的细节: 1,冒号( :)表示继承关系,Parent 表示被继承的类,public 的意义是什么? class Parent { }; class Child : public Parent ...

随机推荐

  1. java 如何实现开箱即用的敏感词控台服务?

    sensitive-word-admin sensitive-word-admin 是基于 sensitive-word 实现的, 一款开箱即用的敏感词控台服务. 特性 基本的 CRUD 开箱即用的配 ...

  2. 【OpenGL ES】绘制圆形

    1 前言 ​ [OpenGL ES]绘制三角形 中介绍了绘制三角形的方法,[OpenGL ES]绘制正方形中介绍了绘制正方形的方法,本文将介绍绘制圆形的方法. ​ OpenGL 以点.线段.三角形为图 ...

  3. 【Android 逆向】【攻防世界】easyjava

    1. apk 安装到手机,提示输入flag 2. jadx 打开apk看看 private static char a(String str, b bVar, a aVar) { return aVa ...

  4. collections模块下的defaultdict用法

    defaultdict from collections import defaultdict s=[('yellow',1),('blue', 2), ('yellow', 3), ('blue', ...

  5. 【LeetCode回溯算法#04】组合总和I与组合总和II(单层处理位置去重)

    组合总和 力扣题目链接(opens new window) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target ...

  6. 【LeetCode哈希表#5】四数相加II(map)

    四数相加II 力扣题目链接(opens new window) 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + ...

  7. ASP.NET Core MVC应用模型的构建[4]: Action的选择

    ControllerModel类型的Actions属性包含一组描述有效Action方法的ActionModel对象.对于定义在Controller类型中的所有方法,究竟哪些方法才能成为有效的Actio ...

  8. spring 注入参数时为list map写法用例

    导包基础:这了让服务器支持json 需要导入下面包 <dependency> <groupId>com.alibaba</groupId> <artifact ...

  9. Abp.Zero 手机号免密登录验证与号码绑定功能的实现(三):Vue网页端开发

    前端代码的框架采用vue.js + elementUI 这套较为简单的方式实现,以及typescript语法更方便阅读. 首先来编写发送验证码函数, 登录,绑定,解绑的业务都需要发送验证码功能,通过c ...

  10. notion database 必知必会

    notion database 必知必会 用过 mysql 的同学一定很容易上手 notion .在 notion 中,掌握好 database,基本上就掌握了 notion 最核心的概念. noti ...