一、
//构造函数
struct student{
string name;
int age;
student(){}
student(string n,int a):name(n),age(a){}
};
int main(){
student mike=student("Mike",20);
cout<<mike.name<<","<<mike.age<<endl;
student who;
cout<<who.name<<","<<who.age<<endl;
return 0;
}
二、删去student(){}
会导致如果不定义初始化的值,就无法创建新的struct。
但其实原来是可以的。
//构造函数
struct student{
string name;
int age;
//student(){}
//student(string n,int a):name(n),age(a){}
};
int main(){
//student mike=student("Mike",20);
//cout<<mike.name<<","<<mike.age<<endl;
student who;
cout<<who.name<<","<<who.age<<endl;
return 0;
}