C++筆記 第四十六課 繼承中的構造與析構---狄泰學院
阿新 • • 發佈:2018-12-01
如果在閱讀過程中發現有錯誤,望評論指正,希望大家一起學習,一起進步。
學習C++編譯環境:Linux
第四十六課 繼承中的構造與析構
1.思考
如何初始化父類成員?父類建構函式和子類建構函式有什麼關係?
2.子類物件的構造
子類中可以定義建構函式
子類建構函式
必須對繼承而來的成員進行初始化
直接通過初始化列表或者賦值的方式進行初始
呼叫父類建構函式進行初始化
父類建構函式在子類中的呼叫方式
預設呼叫
適用於無參建構函式和使用預設引數的建構函式
顯示呼叫
通過初始化列表進行呼叫
適用於所有父類解構函式
父類解構函式的呼叫
46-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) { cout << "Child(string s) :" << s << endl; } }; int main() { Child c; //error, no Parent constructor to call Child cc("cc"); // c output: // "Parent()" // Child() // cc output: // Parent(string s) : cc //Child(string s): cc return 0; } 執行結果 Parent() Child() Parent(string s) : cc Child(string s) : cc
構造規則
子類物件在建立時會首先呼叫父類的建構函式
先執行父類建構函式再執行子類的建構函式
父類建構函式可以被隱式呼叫或者顯式呼叫
物件建立時建構函式的呼叫順序
1.呼叫父類的建構函式
2.呼叫成員變數的建構函式
3.呼叫類自身的建構函式
口訣:先父母,後客人,再自己
46-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 m01; Object m02; public: Child() :m01("Default 1"), m02("Default 2") { cout << "Child()" << endl; } Child(string s) : Parent(s),m01(s + "1"),m02(s + "2") { cout << "Child(string s) : " << s << endl; } }; int main() { Child cc("cc"); // cc output: // Object(string s ) : cc // Parent(string s) : cc // Object(string s) : cc 1 // Object(string s) : cc 2 // Child(string s) : cc return 0; } 執行結果 Object(string s ) : cc Parent(string s) : cc Object(string s ) : cc1 Object(string s ) : cc2 Child(string s) : cc
3.子類物件的析構
解構函式的呼叫順序與建構函式相反
1.執行自身的解構函式
2.執行成員變數的解構函式
3.執行父類的解構函式
46-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 m01; Object m02; string ms; public: Child() :m01("Default 1"), m02("Default 2") { cout << "Child()" << endl; ms = "Default"; } Child(string s) : Parent(s),m01(s + "1"),m02(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 ) : cc1 Object(string s ) : cc2 Child(string s) : cc ~Child() : cc ~Object() : cc2 ~Object() : cc1 ~Parent() : cc ~Object() : cc
小結
子類物件在建立時需要呼叫父類建構函式進行初始化
先執行父類建構函式然後執行成員的建構函式
父類建構函式顯示調用需要在初始化列表中進行
子類物件在銷燬時需要呼叫父類解構函式進行清理
析構順序與構造順序對稱相反