一、類是一個數據類型,因此類定義後須要加;ios
二、初始化列表程序員
1 #include<iostream> 2 using namespace std; 3 class A{ 4 public: 5 A(){ 6 i =5; 7 } 8 void show(){ 9 cout<<i<<endl; 10 } 11 private: 12 int i; 13 }; 14 class B{ 15 public: 16 B():i(5){ 17 } 18 void show(){ 19 cout<<i<<endl; 20 } 21 private: 22 int i; 23 }; 24 int main(){ 25 A().show(); 26 B().show(); 27 } 28 /* 29 輸出:5 30 5 31 C++語言規定,不能在類體中直接指定數據成員的初值,因此對象的初始化工做 32 只能經過調用對象的構造函數來完成。在構造函數中,初始化列表扮演了十分 33 重要的角色。 34 對於普通的數據成員來言,使用初始化列表和在構造函數體內賦值,效果同樣 35 36 */
1 #include<iostream> 2 using namespace std; 3 class A{ 4 int num; 5 public: 6 A(int i){ 7 num = i; 8 } 9 void show(){ 10 cout<<num<<endl; 11 } 12 }; 13 class B{ 14 int &r; 15 const double PI; 16 A a; 17 public: 18 B(int i); 19 void show(){ 20 cout<<r<<endl; 21 cout<<PI<<endl; 22 a.show(); 23 } 24 }; 25 int e = 5; 26 B::B(int i):r(e),PI(3.1515926),a(i){ 27 } 28 int main(){ 29 B(1).show(); 30 } 31 32 /* 33 有些狀況:只能使用初始化列表對成員進行初始化。 34 如:數據成員是引用、常變量、類對象 35 */
1 #include<iostream> 2 using namespace std; 3 class A{ 4 public: 5 int num; 6 A(int i){ 7 num = i; 8 cout<<"In A constructor"<<endl; 9 } 10 }; 11 class B{ 12 public: 13 int num; 14 B(int i){ 15 num = i; 16 cout<<"In B constructor"<<endl; 17 } 18 }; 19 class C{ 20 protected: 21 int num; 22 public: 23 C(int i){ 24 num = i; 25 cout<<"In C constructor"<<endl; 26 } 27 28 }; 29 class D:public C{ 30 A a; 31 int num; 32 B b; 33 public: 34 D(int i):num(i++),b(i++),a(i++),C(i++){} 35 void show(){ 36 cout<<"C::num="<<C::num<<endl;//由於C爲基類,因此先執行基類的構造函數 第一個構造 37 cout<<"a.num="<<a.num<<endl;//a爲數據成員,按聲明順序進行構造。 第二個構造 38 cout<<"num="<<num<<endl;//在初始化列表中存在,按聲明順序構造,爲第三個構造 39 cout<<"b.num"<<b.num<<endl;//初始化列表中存在,按聲明順序構造,爲第四個構造 40 } 41 }; 42 int main(){ 43 D d(1); 44 d.show(); 45 } 46 47 /* 48 不少類的構造函數根本沒有使用初始化列表。但實際上,有些內容無論有沒有顯式 49 寫進初始化列表中,都是會被「強行」加進去。 50 如: 51 一、若是該類是某個類的派生類,那麼它的直接基類的構造函數必定要出如今初始化列表中。 52 要麼是程序員顯式地在初始化列表中調用直接類的構造函數, 53 不然編譯器把直接基類的默認構造函數插入到初始化列表中。 54 二、若是該類包含其餘類的對象做爲其數據成員,那麼這些對象的初始化 55 工做也必定要在初始化列表中完成, 56 要麼是程序員顯式地在初始化列表表中給出對象的構造形式, 57 不然編譯器會在初始化列表中調用這些對象的默認構造函數來完成初始化。 58 59 遵循規則: 60 基類的構造函數最早執行,其餘的成員按照它們在類中聲明的順序依次 61 被初始化。 62 */
1 #include<iostream> 2 using namespace std; 3 int *CreateArr(int n){ 4 int *p; 5 p = new int[n]; 6 for(int i=0;i<n;i++){ 7 p[i] = i+1; 8 } 9 return p; 10 } 11 class A { 12 int arrsize; 13 const int*const arr; 14 public: 15 A(int n):arr(CreateArr(n)){ 16 arrsize = n; 17 } 18 void show(){ 19 for(int i =0;i<arrsize;i++){ 20 cout<<arr[i]<<' '; 21 } 22 cout<<endl; 23 } 24 ~A(){delete[] arr;} 25 }; 26 int main(){ 27 A a(3); 28 a.show(); 29 } 30 /* 31 在初始化列表中,沒法完成爲對象的數組成員進行初始化工做,緣由是C++語言沒有提供這樣的機制。 32 在類中不能定義一個常量數組,由於沒法爲對象的常量數組成員初始化。 33 34 在實際應用中,一般只會在類中定義靜態的常量數組,這樣就能能夠完成數組的初始化工做。 35 或者在類中定義一個指向常量的指針常量,而後在初始化列表中爲它初始化。 36 37 */
三、構造函數與申請空間數組
1 #include<iostream> 2 using namespace std; 3 class B; 4 class A{ 5 public: 6 A(B &obj){ 7 cout<<"Befor constructor,b's address is:"<<&obj<<endl; 8 } 9 }; 10 class B{ 11 int num; 12 public: 13 B(){ 14 cout<<"In constructor,b's address is:"<<this<<endl; 15 } 16 void show(){ 17 cout<<num<<endl; 18 } 19 }; 20 extern B b;//避免重複定義,它會尋找其餘b 21 A a(b); 22 B b; 23 int main(){ 24 cout<<"After constructor,b's address is:"<<&b<<endl; 25 } 26 /* 27 輸出: 28 Befor constructor,b's address is:00D8F218 29 In constructor,b's address is:00D8F218 30 After constructor,b's address is:00D8F218 31 32 對象分配空間和調用對象的構造函數是兩個不一樣的工做。 33 構造函數不是用來爲對象分配空間的,而是對數據成員 34 進行初始化的。使它們首次得到有意義的值。 35 進入對象的構造函數時,對象的空間分配的工做已經完成。 36 */