结构体
1.结构体的三种定义
(1)struct 结构体名 变量名
(2)struct 结构体名 变量名 = (成员1值,成员2值…)
(3)定义结构体时顺便创建变量
struct Student
{
//成员类型集合
string name;
int age;
int score;
} s3;
#include <iostream>
#include <string>
using namespace std;
//1.创建学生数据类型——包括 姓名,年龄 ,分数
//【提醒】末尾括号后需要些写分号;
struct Student
{
//成员类型集合
string name;
int age;
int score;
};
//2.通过学生类型创建具体学生
//2.2 struct Student s2 =(....);
//2.3 定义结构体时顺便创建变量
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;
return 0;
}