//定义结构体类型
struct Person{
string name;
int age;
double heigth;
};
初始化方式:
1.Person per = {"yanhe",18,170};
cout<<per.name; //输出:yanhe
2. Person per ;
per.name="yanhe";
cout<<per.name //输出:yanhe
3. Person per{.age=20};
cout<<per.age //输出20
4. 定义时就初始化:
第一种方式初始化方式不带参数:
struct Person{
string name;
int age;
double heigth;
Person(){
name="haha";
age=20;
}
~Person(){ //析构函数
printf("xiaohuil");
}
};
Person person;
cout<<person.age; //输出20
第二种方式初始化方式带参数:
struct Person{
string name;
int age;
double heigth;
Person(string n,int a,double h){
name= n;
age= a;
heigth= h;
}
};
Person person("yanhe",20,170);
cout<<person.age; //输出20
5.定义时就定义结构体变量时的初始化:
struct Person{
string name;
int age;
double heigth;
}person;
person.name = "yanhe";
cout<<person.name; //输出yanhe
//其它种用法同上