结构体嵌套结构体(4)

时间:2020-04-13 12:43:34   收藏:0   阅读:57

作用:结构体中的成员可以是另一个结构体

例如:每个老师辅导一个成员,一个老师的结构体中,记录另一个学生的结构体

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 //定义学生结构体
 6 struct Student
 7 {
 8     string name;
 9     int age;
10     int score;
11 };
12 
13 //定义老师结构体
14 struct Teacher 
15 {
16     string name;
17     int id;
18     int age;
19 
20     struct Student stu;//创建结构体变量
21 };
22 
23 
24 int main(void)
25 {
26     //老师结构体初始化
27     struct Teacher t;
28     t.name = "小李";
29     t.id = 10000;
30     t.age = 22;
31     t.stu.name = "张三";
32     t.stu.age = 18;
33     t.stu.score = 60;
34 
35     cout << "老师:"<< t.name << " ID:" << t.id << " 年龄" << t.age << endl;
36     cout << "学生:" << t.stu.name << " 年龄:" << t.stu.age << " 分数:" << t.stu.score << endl;
37 
38     system("pause");
39     return 0;
40 }

 

原文:https://www.cnblogs.com/huanian/p/12690570.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!