1. 程式人生 > >構造函數的初始化表

構造函數的初始化表

!= tail name you lse let 函數 space emp

構造函數的初始化表

構造函數有個特殊的初始化方式叫“初始化表達式表”(簡稱初始化表)。初始化表 位於函數參數表之後,卻在函數體 {} 之前。這說明該表裏的初始化工作發生在函數體 內的任何代碼被執行之前。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 
 6 int main(int argc, char** argv) {
7 int i; 8 //定義名為student的遞歸結構 9 struct student { 10 char name[10]; 11 int math; 12 int computer; 13 float sum; 14 student *next; //next成員是指向自身的結構指針 15 }; 16 17 //用student聲明3個結構指針變量 18 struct student *head,*tail,*temp;
19 20 //申請第1塊數據,並設置各結構指針的初值 21 temp=new struct student; //申請內存 22 head=temp; // 頭指針 23 tail=head; // 尾指針 24 25 //循環為鏈表輸入數據 26 cout<<"\tname Math Computer"<<endl; 27 for (i=1;;i++) { 28 cout<<i<<"\t"; 29 cin>>temp->name;
30 if (temp->name[0]!=*) 31 { 32 cin>>temp->math>>temp->computer; 33 temp->sum=temp->math+temp->computer; 34 temp->next=NULL; 35 tail=temp; //設置鏈表尾指針 36 } 37 else 38 { 39 // 以下是輸入結束處理 40 delete temp; 41 tail->next=NULL; 42 break; 43 } 44 //為下一個學生申請內存 45 temp->next=new struct student; 46 temp=temp->next; // 使處理指針temp指向新內存塊 47 } 48 49 //將鏈表數據從頭到尾打印出來 50 cout<<"--------------------"<<endl; 51 temp=head; 52 while (temp!=NULL) { 53 cout<<temp->name<<","<<temp->math<<","; 54 cout<<temp->computer<<","<<temp->sum<<endl; 55 temp=temp->next; 56 } 57 }

構造函數的初始化表