#include <iostream>
using namespace std;
#include <string>
// 作用:结构体中的成员可以是另一个结构体
// 例子:每个老师辅导一个学生,一个老师的结构体中,记录一个学生的结构体
struct student {
string name;
int age;
int score;
};
struct teacher {
int id;
string name;
int age;
int score;
struct student stu;
};
int main()
{
// 结构体嵌套结构体
// 创建老师
teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.score = 100;
t.stu.name = "小王";
t.stu.age = 25;
t.stu.score = 99;
cout << "老师姓名:" << t.name << " 老师编号:" << t.id << " 老师年龄:" << t.age << " 老师分数:" << t.score << endl;
cout << "老师辅导的学生:" << t.stu.name << " 学生的年龄:" << t.stu.name << " 学生的分数:" << t.stu.score << endl;
system("pause");
return 0;
}
结构体中嵌套结构体
最新推荐文章于 2024-07-11 10:20:14 发布