一、为了更好的说明初始化列表,首先由一个错误的代码引入:
1 #include<iostream> 2 3 #include"string.h" 4 using namespace std; 5 6 class Student 7 { 8 private: 9 int Num,Chinese,Maths,English; 10 float Total; 11 char Name[20]; 12 public: 13 Student(){}//默认构造函数 14 Student(char name[20],int num,int chinese,int maths,int english); 15 16 }; 17 Student::Student(char name[20],int num,int chinese,int maths,int english){ 18 int n; 19 for(n=0;n<strlen(name);n++) 20 Name[n]=name[n]; 21 Num=num; 22 Chinese=chinese; 23 English=english; 24 Maths=maths; 25 Total=maths+chinese+english; 26 } 27 28 int main() 29 { 30 int i,j; 31 int num,chinese,maths,english; 32 char name[20]; 33 Student std[5];//因为有了默认构造函数所以此时编译可以通过 34 35 for(i=0;i<5;i++) 36 { 37 cin>>name[20]>>num>>chinese>>maths>>english; 38 std[i](name[20],num,chinese,maths,english); 39 for(j=0;j<20;j++) 40 name[j]='