【C++深度剖析教程25】继承中的构造与析构
今天來學習C++中繼承的構造與析構,有興趣一起學習的加qq:1126137994
1、問題
如何初始化父類成員?父類構造函數與子類構造函數有什么關系?
子類對象是如何構造的?
初始化的方法有兩種:
-直接通過初始化列表或者賦值的方式進行初始化
-調用父類的構造函數進行初始化
父類構造函數在子類中的調用方式
-適用于無參構造函數和使用默認參數的構造函數
-通過初始化列表進行調用
-適用于所有父類構造函數
調用方式如圖所示:
下面還是寫一個簡單的程序來初探子類構造函數:
#include <iostream> #include <string>using namespace std;class Parent { public:Parent(){cout << "Parent()" << endl;}Parent(string s){cout << "Parent(string s) : " << s << endl;} };class Child : public Parent { public:Child(){cout << "Child()" << endl;}Child(string s) : Parent(s) //這里是Child類繼承Parent類的帶參數的構造函數{cout << "Child(string s) : " << s << endl;} };int main() { Child c; Child cc("cc");return 0; }上面的程序的運行結果為:
Parent()
Child()
Parent(string s) : cc
Child(string s) : cc
由以上程序分析可知:
子類對象的構造的規則:
那么構造函數的調用順序是什么呢?
- 調用父類構造函數
- 調用成員變量的構造函數
調用類自身的構造函數
總結為一句口訣:
先父母,后客人,再自己!!!
2、 子類構造的深度解析
下面再看一個例子,讓我們理解一下子類構造函數的深層意義:
#include <iostream> #include <string>using namespace std;class Object { public:Object(string s){cout << "Object(string s) : " << s << endl;} };class Parent : public Object { public:Parent() : Object("Default"){cout << "Parent()" << endl;}Parent(string s) : Object(s){cout << "Parent(string s) : " << s << endl;} };class Child : public Parent {Object mO1;Object mO2; public:Child() : mO1("Default 1"), mO2("Default 2"){cout << "Child()" << endl;}Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2"){cout << "Child(string s) : " << s << endl;} };int main() { Child cc("cc");return 0; }編譯運行結果為:
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
由以上程序的運行結果分析:
以上的分析剛好印證了那句口訣:
子類調用構造函數的順序的口訣為:
先父母,后客人,再自己!!!
3、子類對象的析構 ##
析構函數的調用順序與構造函數的調用順序剛好相反
- 執行自身的析構函數
- 執行成員變量的析構函數
- 執行父類的析構函數
下面還是以一個簡單的示例程序來說明這一點:
#include <iostream> #include <string>using namespace std;class Object {string ms; public:Object(string s){cout << "Object(string s) : " << s << endl;ms = s;}~Object(){cout << "~Object() : " << ms << endl;} };class Parent : public Object {string ms; public:Parent() : Object("Default"){cout << "Parent()" << endl;ms = "Default";}Parent(string s) : Object(s){cout << "Parent(string s) : " << s << endl;ms = s;}~Parent(){cout << "~Parent() : " << ms << endl;} };class Child : public Parent {Object mO1;Object mO2;string ms; public:Child() : mO1("Default 1"), mO2("Default 2"){cout << "Child()" << endl;ms = "Default";}Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2"){cout << "Child(string s) : " << s << endl;ms = s;}~Child(){cout << "~Child() " << ms << endl;} };int main() { Child cc("cc");cout << endl;return 0; }運行結果為:
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
~Child() cc
~Object() : cc 2
~Object() : cc 1
~Parent() : cc
~Object() : cc
由運行結果可以很明顯看出,析構函數的調用書序,就是與構造函數的調用順序相反。
4、總結
想獲得各種學習資源以及交流學習的加我(有我博客中寫的代碼的原稿):
qq:1126137994
微信:liu1126137994
可以共同交流關于嵌入式,操作系統,C++語言,C語言,數據結構等技術問題。
總結
以上是生活随笔為你收集整理的【C++深度剖析教程25】继承中的构造与析构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SaaSBase:最受欢迎的跨境电商软件
- 下一篇: s3c2440移植MQTT