一、结构体嵌套结构体
结构体内的成员可以是另一个结构体(访问时用.访问到不能访问为止)
1、例如:每个老师指导一个学员,一个老师的结构体中嵌套一个学生的结构体
#include<iostream>
using namespace std;
#include<string>
//结构体嵌套结构体
//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
int main()
{
//创建老师
teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu.name = "小汪";
t.stu.age = 18;
t.stu.score = 100;
cout << "老师姓名:" << t.name << " " << "老师职工编号:" << t.id << " " << "老师的年龄:" << t.age << endl;
cout << "老师辅导的学生姓名:" << t.stu.name << " " << "学生的年龄:" << t.stu.age << " " << "学生的分数:" << t.stu.score