c++基础知识第十天:结构体嵌套结构体,结构体作函数参数

一、结构体嵌套结构体

结构体内的成员可以是另一个结构体(访问时用.访问到不能访问为止)

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值