结构体 — 嵌套结构体_其他

 

结构体 — 嵌套结构体_#include_02

注意:

在结构体中可以定义另一个结构体作为成员,用来解决实际问题

先定义 子结构体,后定义 父结构体

结构体 — 嵌套结构体_子结构_03

点击查看代码
#include<iostream>
#include<string> 

using namespace std;

//先定义 子 结构体
struct student
{
	//成员列表
	string name;

	int age;

	int score;

};
//后定义 父 结构体
struct teacher
{
	//成员列表
	int id;
	string name;
	int age;

	struct student stu;
};



int main(){
	
	//结构体嵌套结构体
	teacher t;
	t.age = 60;
	t.stu.name = "张三"; 
	t.stu.age = 18;

	cout << "老师的年龄:" << t.age  << ";老师的学生姓名:" << t.stu.name << endl;

	system("pause");

	return 0;
}