C++结构体嵌套结构体

875 阅读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(){
    
    //创建老师
    struct teacher t;
    t.id = 10000;
    t.name = "老王";
    t.age = 50;
    t.stu.name = "小王";
    t.stu.age = 20;
    t.stu.score = 88;
    
    //输出

    system("pause");
    return 0;
}