语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:
- struct 结构体名 变量名
- struct 结构体名 变量名 = { 成员值1,成员值2…}
- 定义结构体时顺便创建变量
示例:
#include<iostream>
#include<string>
using namespace std;
//1、创建学生的数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 struct 类型名称 {成员列表 }
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
} s3;//顺便定义 少用
//2、通过学生类型创建具体学生(结构体创建中的struct可以省略 定义时不行)
int main(){
//2.1 struct Student s1
struct Student s1;
//给s1属性赋值,通过访问.(点)结构体变量中的属性
s1.name= "张三";
s1.age=18;
s1.score=100;
cout<<"姓名:"<<s1.name<<" 年龄:"<<s1.age<<" 分数:"<<s1.score<<endl;
//2.2 struct Student s2={...}
struct Student s2={"李四&#