您好,登錄后才能下訂單哦!
繼承
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。