您好,登录后才能下订单哦!
继承
1、单继承
class Base { public: Base() { cout << "Base()" << this << endl; } void FunTest1() { cout << "Base::FunTest1()" << endl; } void FunTest2() { cout << "Base::FunTest2()" << endl; } ~Base() { cout << "~Base()" << endl; } int _data1; }; class Derive :public Base { public: void FunTest3() { cout << "Derive::FunTest3()" << endl; } int _data2; }; int main() { Base b; b.FunTest1(); b.FunTest2(); b._data1 = 0x04; Derive d; d.FunTest1(); d.FunTest2(); d.FunTest3(); d._data1 = 0x01; d._data2 = 0x02; return 0; }
——b对象和d对象的对象模型
main 函数的反汇编
派生类的构造函数:
程序运行结果:
构造函数:
基类构造函数:Base( )884 ->派生类构造函数: Base( )874 ->Derive( )874
析构函数:
派生类的析构函数:~Derive( )874 -> ~Base( )874->基类的析构函数:~Base( )884
2、多继承
class Base { public: Base() { cout << "Base()" << this << endl; } void FunTest1() { cout << "Base::FunTest1()" << endl; } void FunTest2() { cout << "Base::FunTest2()" << endl; } ~Base() { cout << "~Base()" << endl; } int _data1; }; class Base1 { public: Base1() { cout << "Base1()" << endl; } void FunTest3() { cout << "Base::FunTest3()" << endl; } void FunTest4() { cout << "Base::FunTest4()" << endl; } int _data3; }; class Derive :public Base,public Base1 { public: Derive() { cout << "Derive()" << this << endl; } void FunTest5() { cout << "Derive:FunTest5()" << endl; } ~Derive() { cout << "~Derive()" << endl; } int _data2; }; int main() { Base b; b.FunTest1(); b.FunTest2(); b._data1 = 0x04; Base1 b1; b1.FunTest3(); b1.FunTest4(); b1._data3 = 0x05; Derive d; d.FunTest1(); d.FunTest2(); d.FunTest3(); d.FunTest4(); d.FunTest5(); d._data1 = 0x01; d._data2 = 0x02; d._data3 = 0x03; return 0; }
main函数的反汇编:
派生类的构造函数:
先调用Base的构造函数,再调用Base1的构造函数
析构函数的调用:
先是派生类的析构函数,再试Base1的析构函数,最后是Base的析构函数
在派生类的析构函数中又会调用基类的析构函数:
在派生类的析构函数中,也是先调用Base1的析构函数,再调用Base的析构函数。与构造函数的顺序正好相反
程序运行的结果:
构造函数:
j基类的构造函数:Base( )D9c -> Base1( )D90 -> 派生类中的构造函数:Base( )D7c -> Base1 ( )D80 -> Derive( )D7c
析构函数:
派生类中的析构函数:~Derive( )D7c -> ~Base1( )D80 -> ~Base( )D7c ->基类的析构函数:~Base1( )D90 -> ~Base( )D9C
3、菱形继承
class Base { public : Base() { cout << "Base()" << this << endl; } void FunTest1() { cout << "Base::FunTest1()" << endl; } ~Base() { cout << "~Base()" << endl; } }; class Derive : public Base { public : Derive() { cout << "Derive()" << this << endl; } void FunTest2() { cout << "Derive:FunTest2()" << endl; } ~Derive() { cout << "~Derive()" << endl; } }; class Derive1 : public Base { public : Derive1() { cout << "Derive1()" << this << endl; } void FunTest3() { cout << "Derive:FunTest4()" << endl; } ~Derive1() { cout << "~Derive1()" << endl; } }; class Derive2 : public Derive ,public Derive1 { public : Derive2() { cout << "Derive2()" << endl; } void FunTest4() { cout << "Derive:FunTest4()" << endl; } }; int main() { Base b; b.FunTest1(); Derive d; d.FunTest1(); d.FunTest2(); Derive1 d1; d1.FunTest1(); d1.FunTest3(); Derive2 d2; //d2.FunTest1();会出现二义性 d2. Derive ::FunTest1(); d2. Derive1 ::FunTest1(); d2.FunTest2(); d2.FunTest3(); d2.FunTest4(); return 0; }
对象模型:
Derive和Derive1中都含有了Base,会产生二义性和数据冗余。
main函数的反汇编:
派生类的构造函数:
在派生类的构造函数中,都先调用的基类的构造函数。
Derive()
Derive1()
Derive2()
析构函数的调用:
析构函数的顺序和构造函数的顺序正好相反。
~Derive2( )->~Derive1( )->~Derive( )->~Base( ),
派生类中析构函数的调用:
在~Derive2(),先是自己的析构函数,再调用了~Derive1(),再调用了~Derive(),
在~Derive1( )中先是自己的析构函数,又调用了~Base()
在~Derive( )中先是自己的析构函数,又调用了~Base()
程序运行结果:
构造函数:
Base( )B8F ->
Derive的构造函数:Base( )B83 -> Derive( )B83->
Derive1的构造函数:Base( )B77 -> Derive1( )B77->
Derive2的构造函数:Base( )B6B -> Derive( )B6B ->Base( )B6C -> Derive1( )B6C ->Derive2( )B6B
析构函数:
~Derive2( )B6B -> ~Derive1( )B6C ->~Base( )B6C -> ~Derive( )B6B -> ~Base( )B6B
~Derive1( )B77 -> ~Base( )B77 -> ~Derive( )B83 -> ~Base( )B83
~Base( )B8F
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。