博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++继承与派生(原理归纳)
阅读量:4481 次
发布时间:2019-06-08

本文共 3795 字,大约阅读时间需要 12 分钟。

   1.   C++继承与java不同,java遵循单继承,但java的接口为其不足做了很好的弥补了。 C++则是灵活的多,为多继承。即一个C++类可以同时继承N个类的属性。

    

2. 对于继承方式 :

    有三种:  public ,private,protect,对于public继承的类,其公有成员依然是公有成员,私有成员依旧是私有成员。  

    对于protect,private 则有限制 ,就好比一个水管,公有水管是最大号的,对于水的流量没有限制。保护水管,是中等的号的,对于大号水管的流量使其变成中等流量,对于中等以下的不限制。私有水管,则是最小号的,对于大于私有水管限制的统统的改为私有水管的标准。 其中私有继承,其实就是一种绝育的措施。就是以后的继承就没有太大意义。

 

3.对于继承关于构造和析构顺序原里的归纳:

看代码:

  

1 #include
2 using namespace std ; 3 4 class Base1 { 5 6 public : 7 Base1() { 8 cout << "Default Base1" << endl; 9 }10 Base1(int i) {11 cout << "Base1"<
<< endl;12 }13 ~Base1() {14 cout << "Base1 析构" << endl;15 }16 };17 18 class Base2 {19 20 public :21 Base2() {22 cout << "Default Base2" << endl;23 }24 ~Base2() {25 cout << "Base2 析构" << endl;26 }27 Base2(int i) {28 cout << "Base2" << i << endl;29 }30 };31 class Base3 {32 33 public :34 Base3() {35 cout << "Default Base3" << endl;36 }37 ~Base3() {38 cout << "Base3 析构" << endl;39 }40 Base3(int i) {41 cout << "Base3" << i << endl;42 }43 44 };45 46 47 class Derived : public Base1, public Base2, public Base3 //(1)先 在这儿开始构造从左到右48 //析构则是从右到左49 {50 51 public :52 Derived() {53 cout << "Default Derived" << endl;54 }55 ~Derived() {56 cout << "Derived 析构" << endl;57 }58 Derived( int a , int b , int c , int d ) 59 :men2(b),Base1(a),Base3(c), Base2(d),men1(b) { 60 cout << "Derived" << endl;61 };62 63 private :64 //构造从左到右65 Base3 men3;66 Base2 men2;67 Base1 men1;68 69 //析构则是从底部开始往上析构,先 Base 1,2,370 };71 72 int main(void ) {73 74 Derived obj(1,2,3,4);75 return 0;76 77 }

结果:

   

Base11Base24Base33Default Base3Base22Base12DerivedDerived 析构Base1 析构Base2 析构Base3 析构Base3 析构Base2 析构Base1 析构请按任意键继续. . .

 

4. 以上是对于单继承的描述,如果是多继承,那么常规的话,我们很容易清楚器执行顺序,但是如果是虚继承,其执行顺序又会如何 ?

    1. 现在我们来看这样一个图:它的运行结果又是如何.......

   

1 #include
2 using namespace std; 3 4 class Boss { 5 6 public : 7 Boss() { 8 cout << "this is Boss's constructor !" << endl; 9 }; 10 Boss(int i) { 11 cout << "this is Boss's constructor !" \ 12 << " moneny=" << i << endl; 13 } 14 15 void show() { 16 cout<<"宝剑磨砺,斩魂妖,时光磨砂,魔刃出"<
show();127 //((Boss)sb).show();128 return 0;129 }

结果为:

this is Boss's constructor !

this is VIP_em's constructor ! moneny=100
this is xiao_er's constructor ! moneny=12
this is er_xiao's constructor ! moneny=13

                                                      -------------这部分为熊孩子的继承部分构造函数

下面是私有变量的构造函数

this is Boss's constructor ! 

this is VIP_em's constructor !

                                  ------私有变量 Vip_em调用无参数的构造函数 

this is Boss's constructor ! moneny=100 
this is xiao_er's constructor ! moneny=100

                                 ------私有变量  xiao_er调用有参数的构造函数 

this is Boss's constructor !

this is xiao_er's constructor !

                                  ------私有变量 xiao_er调用无参数的构造函数 

this is er_xiao's constructor !

this is stupid_kid's constructor ! moneny=100
我是熊孩子,蜀黍,蜀黍,抱抱!
宝剑磨砺,斩魂妖,时光磨砂,魔刃出
this is Boss's xigou function !
this is stupid_kid's xigou function !
this is er_xiao's xigou function !
this is xiao_er's xigou function !
this is Boss's xigou function !
this is xiao_er's xigou function !
this is Boss's xigou function !
this is VIP_em's xigou function !
this is Boss's xigou function !
this is er_xiao's xigou function !
this is xiao_er's xigou function !
this is VIP_em's xigou function !
this is Boss's xigou function !
请按任意键继续. . .

6、 从上述代码可以不难看出,  虚内继承,避免了二义性,仅仅压缩了公有的你虚类继承类。

如果要弄清楚虚拟继承,就得先知道virtual table (vtbl) ----我们说的虚函数表

   在内存那块, 会留 下一块连续的内存块,用作vtble存储JMP地址,而vtble里头存的便是virtual function(虚函数)地址,

每次继承时,基类都会产生一个vptr指针,指向派生类的地质,当 vptr指针指着同一个地址时,就不重复构造。 其他的,构造函数和析构函数,基于第一个代码列子。  根据自己对源码的理解和测试的总结,如有错误,还请多多指正。

 

转载于:https://www.cnblogs.com/gongxijun/p/4356866.html

你可能感兴趣的文章
多态的理解
查看>>
AspNet Core 发布到Linux系统和发布IIS 注意项
查看>>
Windows添加.NET Framework 3.0 NetFx3 失败 - 状态为:0x800f0950
查看>>
隐藏显示终端的光标(shell echo,linux c printf)
查看>>
SQL Server 存储过程
查看>>
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
用Okhttp框架登录之后的Cookie设置到webView中(转)
查看>>
Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
查看>>
elasticsearch 5.x 系列之二 线程池的设置
查看>>
Java入门系列:实例讲解ArrayList用法
查看>>
洛谷P1080 国王游戏【大数】【贪心】
查看>>
Python 字符串相似性的几种度量方法
查看>>
OpenMP编程的任务调度控制
查看>>
卡特兰(Catalan)数列
查看>>
设计模式(一)工厂模式Factory(创建型)
查看>>
Warshall算法
查看>>
Python之匿名函数
查看>>
PhoneGap 3.0 安装
查看>>