9-【快乐学习c++】结构体

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

// 用户自定义数据类型--结构体
//1、创建学生的数据类型
//语法: struct 结构体名称 { 结构体成员列表 }
struct Student
{
    //成员列表

    string name;//姓名
    int age;//年龄
    int score;//分数

}s3;//方式三 不建议

struct Teather
{
    int id; //职工变化
    string name;
    int age;
    struct Student stu; //辅导的学生
};

void PrintTeather_1(struct Teather t);
void PrintTeather_2(struct Teather * t);
void PrintJust(const struct Teather * t);

struct bsStudent
{
    string name;
    int score;
};
int main()
{

    //2、通过学生类型创建具体学生
    cout << "\n\n----------------------------------[part-1 创建结构体]" <<endl;
    //方式一
    struct Student s1;  //struct可以省略
    s1.name = "海绵宝宝";
    s1.age = 18;
    s1.score = 43;
    cout << "姓名: " << s1.name << " 年龄: "<< s1.age << " 分数: "<< s1.score << endl;

    //方式二
    struct Student s2 = {"派大星",13,90};
    cout << "姓名: " << s2.name << " 年龄: "<< s2.age << " 分数: "<< s2.score << endl;

    //方式三
    s3.name = "松鼠珊迪";
    s3.age = 42;
    s3.score = 90;
    cout << "姓名: " << s3.name << " 年龄: "<< s3.age << " 分数: "<< s3.score << endl;

    //3、结构体数组
    cout << "\n\n----------------------------------[part-3 结构体数组]" <<endl;
    // 语法:struct 结构体名称 数组名[元素个数] = { ... }
    //  3-1 创建结构体数组
    struct Student strArr[3] =
    {
        {"章鱼哥",13,31},
        {"蟹老板",14,41},
        {"皮老板",15,51},
    };
    //  3-2 赋值
    strArr[1].name = "派大星";
    strArr[1].age = 22;
    strArr[1].score = 88;
    //  3-3 遍历
    for (int i = 0;i<3;i++)
    {
       cout << "姓名: " << strArr[i].name << " 年龄: "<< strArr[i].age << " 分数: "<< strArr[i].score << endl;

    }

    // 4、结构体指针
    cout << "\n\n----------------------------------[part-4 结构体指针]" <<endl;
    struct Student s6;
    s6.name = "章鱼哥";
    s6.age = 222;
    s6.score = 67;

    // 指针指向结构体变量  语法:-> 可以通过结构体指针访问
    struct Student * p = &s6;
    cout << "姓名: " << p->name << " 年龄: "<<  p->age << " 分数: "<<  p->score << endl;


    // 5、结构体嵌套结构体
    cout << "\n\n----------------------------------[part-5 结构体嵌套]" <<endl;
    // 创建老师
    Teather t1;
    t1.age = 65;
    t1.id = 1;
    t1.name = "泡芙老师";
    t1.stu = strArr[2];
    t1.stu.age = 999;
    cout << "老师姓名: " << t1.name << " 老师年龄: "<<  t1.age <<
    " 老师id: "<< t1.id << "学生姓名:"<< t1.stu.name<<" 学生年龄: "<<
    t1.stu.age << " 学生分数: "<<  t1.stu.score <<endl;


    // 6、结构体作为函数参数
    cout << "\n\n----------------------------------[part-6 结构体作为函数参数]" <<endl;
    // 值传递
    PrintTeather_1(t1);
    // 地址传递
    PrintTeather_2(&t1);

    cout << "老师姓名: " << t1.name << " 老师年龄: "<<  t1.age <<
    " 老师id: "<< t1.id << "学生姓名:"<< t1.stu.name<<" 学生年龄: "<<
    t1.stu.age << " 学生分数: "<<  t1.stu.score <<endl;



    // 7、结构体的const  (防止误操作)
    cout << "\n\n----------------------------------[part-7 结构体的const]" <<endl;
    PrintJust(&t1);

}
//不可以修改
void PrintTeather_1(struct Teather t)
{
    cout << "老师姓名: " << t.name << " 老师年龄: "<<  t.age <<
    " 老师id: "<< t.id << "学生姓名:"<< t.stu.name<<" 学生年龄: "<<
    t.stu.age << " 学生分数: "<<  t.stu.score <<endl;
}
//可以修改
void PrintTeather_2(struct Teather * t)
{
    t->age = 100;
    cout << "老师姓名: " << t->name << " 老师年龄: "<<  t->age <<
    " 老师id: "<< t->id << "学生姓名:"<< t->stu.name<<" 学生年龄: "<<
    t->stu.age << " 学生分数: "<<  t->stu.score <<endl;
}
// 只能打印不能修改
void PrintJust(const struct Teather * t)
{
    // 报错 t->age = 500; //修改就报错
    cout << "老师姓名: " << t->name << " 老师年龄: "<<  t->age <<
    " 老师id: "<< t->id << "学生姓名:"<< t->stu.name<<" 学生年龄: "<<
    t->stu.age << " 学生分数: "<<  t->stu.score <<endl;
}

输出
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107548871