C++继承与派生(原理归纳)
1. C++继承与java不同,java遵循单继承,但java的接口为其不足做了很好的弥补了。 C++则是灵活的多,为多继承。即一个C++类可以同时继承N个类的属性。
2. 对于继承方式 :
有三种: public ,private,protect,对于public继承的类,其公有成员依然是公有成员,私有成员依旧是私有成员。
对于protect,private 则有限制 ,就好比一个水管,公有水管是最大号的,对于水的流量没有限制。保护水管,是中等的号的,对于大号水管的流量使其变成中等流量,对于中等以下的不限制。私有水管,则是最小号的,对于大于私有水管限制的统统的改为私有水管的标准。 其中私有继承,其实就是一种绝育的措施。就是以后的继承就没有太大意义。
3.对于继承关于构造和析构顺序原里的归纳:
看代码:
#include<iostream>
using namespace std ;
class Base1 {
public :
Base1() {
cout << "Default Base1" << endl;
}
Base1(int i) {
cout << "Base1"<<i<< endl;
}
~Base1() {
cout << "Base1 析构" << endl;
}
};
class Base2 {
public :
Base2() {
cout << "Default Base2" << endl;
}
~Base2() {
cout << "Base2 析构" << endl;
}
Base2(int i) {
cout << "Base2" << i << endl;
}
};
class Base3 {
public :
Base3() {
cout << "Default Base3" << endl;
}
~Base3() {
cout << "Base3 析构" << endl;
}
Base3(int i) {
cout << "Base3" << i << endl;
}
};
class Derived : public Base1, public Base2, public Base3 //(1)先 在这儿开始构造从左到右
//析构则是从右到左
{
public :
Derived() {
cout << "Default Derived" << endl;
}
~Derived() {
cout << "Derived 析构" << endl;
}
Derived( int a , int b , int c , int d )
:men2(b),Base1(a),Base3(c), Base2(d),men1(b) {
cout << "Derived" << endl;
};
private :
//构造从左到右
Base3 men3;
Base2 men2;
Base1 men1;
//析构则是从底部开始往上析构,先 Base 1,2,3
};
int main(void ) {
Derived obj(1,2,3,4);
return 0;
}
结果:
Base11
Base24
Base33
Default Base3
Base22
Base12
Derived
Derived 析构
Base1 析构
Base2 析构
Base3 析构
Base3 析构
Base2 析构
Base1 析构
请按任意键继续. . .
4. 以上是对于单继承的描述,如果是多继承,那么常规的话,我们很容易清楚器执行顺序,但是如果是虚继承,其执行顺序又会如何 ?
1. 现在我们来看这样一个图:它的运行结果又是如何.......
#include<iostream>
using namespace std;
class Boss {
public :
Boss() {
cout << "this is Boss's constructor !" << endl;
};
Boss(int i) {
cout << "this is Boss's constructor !" \
<< " moneny=" << i << endl;
}
void show() {
cout<<"宝剑磨砺,斩魂妖,时光磨砂,魔刃出"<<endl;
}
virtual ~ Boss() {
cout << "this is Boss's xigou function !" << endl;
}; //虚析构函数
};
//店小二
class xiao_er :virtual public Boss
{
public:
xiao_er() {
cout << "this is xiao_er's constructor !"<<endl;
}
xiao_er(int i) : Boss(i){
cout << "this is xiao_er's constructor !" \
<< " moneny=" << i << endl;
}
virtual ~xiao_er() {
cout << "this is xiao_er's xigou function !" << endl;
}
void show() {
cout << "我是店小二,客官 !" << endl;
}
};
//王二小
class er_xiao : virtual public Boss \
, virtual public xiao_er /*其实这里这个可以省去,但是这儿是为了写代码而写代码*/
{
public :
er_xiao() {
cout << "this is er_xiao's constructor !" << endl;
}
er_xiao(int i) : \
Boss(i) , xiao_er(i+1)
{
cout << "this is er_xiao's constructor !" \
<< " moneny=" << i << endl;
}
virtual ~ er_xiao() {
cout << "this is er_xiao's xigou function !"<<endl;
}
void show() {
cout << "我是王二小,为坏人带路的王二小 !" << endl;
}
};
//天朝VIP员工
class VIP_em : virtual public Boss
{
public:
VIP_em(){
cout << "this is VIP_em's constructor !" << endl;
}
VIP_em(int i) : \
Boss(i)
{
cout << "this is VIP_em's constructor !" \
<< " moneny=" << i << endl;
}
virtual ~VIP_em() {
cout << "this is VIP_em's xigou function !" << endl;
}
void show() {
cout << "我是VIP , 我有特权! "<<endl;
}
};
//熊孩子
class stupid_kid : virtual public VIP_em \
, virtual public xiao_er , \
virtual public er_xiao
{
public:
stupid_kid() {
cout << "this is stupid_kid's constructor !" << endl;
}
stupid_kid(int i) : \
VIP_em(i) , xiao_er(12) , er_xiao(13),xe(i)
{
cout << "this is stupid_kid's constructor !" \
<<" moneny="<<i<<endl;
}
~stupid_kid() {
cout << "this is stupid_kid's xigou function !"<<endl;
}
void show() {
cout << "我是熊孩子,蜀黍,蜀黍,抱抱!" << endl;
}
private :
VIP_em vi;
xiao_er xe;
er_xiao ex;
};
int main(){
stupid_kid st(100);
//父类的函数被覆盖了
st.show();
//如何调用父类,强制是一种。
((Boss)st).show();
//stupid_kid *pt = &st;
//stupid_kid &sb = st;
// pt->show();
//((Boss)sb).show();
return 0;
}
结果为:
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指针指着同一个地址时,就不重复构造。 其他的,构造函数和析构函数,基于第一个代码列子。 根据自己对源码的理解和测试的总结,如有错误,还请多多指正。
编程是一种快乐,享受代码带给我的乐趣!!!