1. 程式人生 > >C++基類和派生類的構造函數和析構函數的調用

C++基類和派生類的構造函數和析構函數的調用

str 生命 ons stream all 兩種 col 生命期 析構函數

C++基類和派生類的構造函數和析構函數的調用

1.調用順序

  當創建一個派生類的對象時,系統首先自動創建一個基類對象,也就是說,在調用派生類構造函數創建派生類對象之前,系統首先調用基類的構造函數創建基類對象。當派生類對象生命期結束時,首先調用派生類的析構函數,然後調用基類的析構函數。

  所以,構造函數:基類->派生類;析構函數:派生類->基類。

  示例:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class A
 5 {
 6 public:
 7     A()
 8     {
 9         cout << "
Based class A constructor is called" << endl; 10 } 11 ~A() 12 { 13 cout << "Based class A destructor is called" << endl; 14 } 15 }; 16 class B :public A 17 { 18 public://不寫public,默認是private 19 B() 20 { 21 cout << "Derived class B constructor is called
" << endl; 22 } 23 ~B() 24 { 25 cout << "Derived class B destructor is called" << endl; 26 } 27 }; 28 29 int main() 30 { 31 B b; 32 33 //system("pause");如果寫這句,VS運行結果只會顯示構造函數,不會顯示析構函數 34 return 0;//將上句註釋,在這句加斷點,手動往下執行一步,可以看到析構函數 35 36 }

  【運算結果】

技術分享

2.隱式調用和顯式調用

  通過派生類的構造函數調用基類的構造函數有兩種方式:隱式調用和顯式調用。

  所謂隱式調用,就是在派生類的構造函數中不指定對應的基類的構造函數,這個時候調用的是基類的默認構造函數(即含有默認參數值或不帶參數的構造函數)

  而所謂顯式調用,就是在派生類的構造函數中指定要調用的基類的構造函數,並將派生類構造函數的部分參數值傳遞給基類構造函數。

  註:除非基類有默認的構造函數,否則必須采用顯式調用。

2.1隱式調用示例

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class A
 5 {
 6 public:
 7     A(int x=0,int y=0)
 8     {
 9         a = x; b = y;
10         cout << "Based class A constructor is called" << endl;
11     }
12     ~A()
13     {
14         cout << "Based class A destructor is called" << endl;
15     }
16 private:
17     int a;
18     int b;
19 };
20 //基類A有默認的構造函數,可以隱式調用
21 class B :public A
22 {
23 public://不寫public,默認是private
24     B(int z=0)//B(int z)不能作為默認構造函數所以不可以,B()能作為默認構造函數所以也可以
25     {
26         c = z;
27         cout << "Derived class B constructor is called" << endl;
28     }
29     ~B()
30     {
31         cout << "Derived class B destructor is called" << endl;
32     }
33 private:
34     int c;
35 };
36 int main()
37 {
38     B b;
39     //system("pause");如果寫這句,VS運行結果只會顯示構造函數,不會顯示析構函數
40     return 0;//將上句註釋,在這句加斷點,手動往下執行一步,可以看到析構函數
41 
42 }

2.2顯式調用示例

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class A
 5 {
 6 public:
 7     A(int x,int y)
 8     {
 9         a = x; b = y;
10         cout << "Based class A constructor is called" << endl;
11     }
12     ~A()
13     {
14         cout << "Based class A destructor is called" << endl;
15     }
16 private:
17     int a;
18     int b;
19 };
20 //基類A沒有默認的構造函數,其現有的構造函數需要傳遞參數
21 //通過派生類構造函數調用A的構造函數時,必須用顯式調用
22 class B :public A
23 {
24 public://不寫public,默認是private
25     B(int x,int y,int z):A(x,y)//B(int z)不能作為默認構造函數所以不可以,B()能作為默認構造函數所以也可以
26     {
27         c = z;
28         cout << "Derived class B constructor is called" << endl;
29     }
30     ~B()
31     {
32         cout << "Derived class B destructor is called" << endl;
33     }
34 private:
35     int c;
36 };
37 int main()
38 {
39     B b(1,2,3);
40     //system("pause");如果寫這句,VS運行結果只會顯示構造函數,不會顯示析構函數
41     return 0;//將上句註釋,在這句加斷點,手動往下執行一步,可以看到析構函數
42 
43 }

——如有不對的地方,非常歡迎給予指導!

——【感謝】資料來源於http://www.cnblogs.com/krisdy/archive/2009/06/11/1501390.html

C++基類和派生類的構造函數和析構函數的調用