C++裏的繼承和多態(中)——分析單繼承、多繼承、菱形繼承(不含虛函數)

  繼承

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對象的對象模型

wKiom1cPOU7QXaJMAAArxXyNJLA741.png

main 函數的反彙編

wKioL1cPOiWjE4KKAAA3PXoK4kA841.png

spacer.gif

派生類的構造函數:

wKiom1cPOYrz8cmZAAAZpM3b2cA027.png

spacer.gif

程序運行結果:

wKiom1cPOaexLaFrAAAW-HqYtaw388.png

spacer.gif

構造函數:

基類構造函數:Base( )884 ->派生類構造函數: Base( )874 ->Derive( )874

析構函數:

派生類的析構函數:~Derive( )874 -> ~Base( )874->基類的析構函數:~Base( )884

spacer.gif

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;
}

wKioL1cPOyGhMMcRAABAGuqf-cY911.png

spacer.gif

main函數的反彙編:

wKiom1cPOonDry9rAAAitv3Kalw028.png

wKioL1cPO0HSezX2AAAkCEZ_FY8825.png

spacer.gif

spacer.gif

派生類的構造函數:

先調用Base的構造函數,再調用Base1的構造函數

wKioL1cPO26SfidFAAAVc-j2eWA477.png

spacer.gif

析構函數的調用:

先是派生類的析構函數,再試Base1的析構函數,最後是Base的析構函數

wKiom1cPOtLisMVoAAAWo7eAFwA040.png

spacer.gif

在派生類的析構函數中又會調用基類的析構函數:

在派生類的析構函數中,也是先調用Base1的析構函數,再調用Base的析構函數。與構造函數的順序正好相反

wKioL1cPO6WyjU5-AAAV_6NHDxE953.png

spacer.gif

程序運行的結果:

構造函數:

j基類的構造函數:Base( )D9c -> Base1( )D90 -> 派生類中的構造函數:Base( )D7c ->  Base1 ( )D80 -> Derive( )D7c 

析構函數:

派生類中的析構函數:~Derive( )D7c -> ~Base1( )D80 -> ~Base( )D7c  ->基類的析構函數:~Base1( )D90 -> ~Base( )D9C 

wKiom1cPOwXitdmbAAAjhMtURZw689.png

spacer.gif

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,會產生二義性和數據冗餘。

wKiom1cPPBLjcc69AAA-W_yVNqI153.png

spacer.gif

main函數的反彙編:

wKiom1cPPDCyzt0XAAAl7TmD0pk179.png

wKiom1cPPDGTTcByAAAi0Hv2cd8167.png

spacer.gif

spacer.gif


派生類的構造函數:

在派生類的構造函數中,都先調用的基類的構造函數。

Derive()

spacer.gif

wKiom1cPPGWS6kwHAAASz2Ryats331.png

Derive1()

wKioL1cPPR2BJqLTAAAUzLMOxi4819.png

Derive2()

wKioL1cPPR3zSXMwAAAWBU630JI333.png

spacer.gif

spacer.gif

析構函數的調用:

析構函數的順序和構造函數的順序正好相反。

~Derive2( )->~Derive1( )->~Derive( )->~Base( ),

spacer.gif

wKiom1cPPKehUwFpAAAa2Tiy99A604.png

派生類中析構函數的調用:

在~Derive2(),先是自己的析構函數,再調用了~Derive1(),再調用了~Derive(),

wKiom1cPPKiQN5e-AAAU46JpoVA649.png

在~Derive1( )中先是自己的析構函數,又調用了~Base()

wKiom1cPPKjwPwr6AAATUqKMm3s175.png

在~Derive( )中先是自己的析構函數,又調用了~Base()

wKioL1cPPWCitKHsAAAR28Ja3kI011.png

spacer.gif

spacer.gif

spacer.gif

程序運行結果:

構造函數:

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

spacer.gif

wKiom1cPPKnSdHjwAAAurB4DQNU188.png






 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章