构造函数的初始化表

构造函数的初始化表

构造函数有个特殊的初始化方式叫“初始化表达式表”(简称初始化表)。初始化表 位于函数参数表之后,却在函数体 {} 之前。这说明该表里的初始化工作发生在函数体 内的任何代码被执行之前。

 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<<"	name    Math   Computer"<<endl;
27     for (i=1;;i++) {
28         cout<<i<<"	";
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 }
原文地址:https://www.cnblogs.com/borter/p/9406544.html