第 16 课 / 共 22 课
结构体
⏱ 70 分钟 | 前置:第 12 课(平行数组)、第 14 课(指针)| 工具:VSCode + g++
🎯 本课目标
- 体会平行数组的痛点,理解结构体 = 把相关数据打包成一个整体(自定义数据类型)
- 会定义 struct、初始化、用
. 访问成员,知道结构体可以整体赋值
- 会用结构体指针与
->,明白它就是 (*p). 的简写
- 看懂结构体嵌套与
typedef/using 别名,为下节课成绩表项目备好零件
⏱ 时间分配
| 时间 | 环节 |
| 0–5 分钟 | 复习第 15 课 + 作业讲评(重点讲泄漏找茬与 new/delete 改错) |
| 5–15 分钟 | 知识点1:为什么需要结构体 + 定义 |
| 15–27 分钟 | 知识点2:初始化与成员访问(本课重头戏) |
| 27–37 分钟 | 知识点3:结构体指针与 -> |
| 37–50 分钟 | 知识点4:嵌套与 typedef/using |
| 50–65 分钟 | 课堂练习 |
| 65–70 分钟 | 总结 + 布置作业 |
知识点 1:为什么需要结构体(10 分钟)
1.1 平行数组的老痛点
第 12 课通讯录用 names[100] 和 phones[100] 两条平行数组,靠下标对齐表示同一个人。删除时两条数组都要前移,漏一条就"张冠李戴";再加个年龄、成绩,就是四条、五条数组一起维护——数据明明是一体的,代码里却散落各处。
🧭 类比学生档案袋:姓名、年龄、分数本来就该装在同一个档案袋里,搬走一起搬走,复印一起复印。结构体就是这个档案袋——把几个相关变量打包成一个新类型。
1.2 定义结构体
student.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student { // struct:声明一种新类型,名字叫 Student
string name; // 成员1:姓名
int age; // 成员2:年龄
double score; // 成员3:分数
}; // ← 这个分号万万不能丢!
int main() {
Student s{"张三", 18, 92.5}; // 按成员顺序初始化
cout << s.name << " " << s.age << " " << s.score << endl;
return 0; // 张三 18 92.5
}
📁 Student s(一个档案袋)
s.name"张三"
s.age18
s.score92.5
- struct 定义的是类型(图纸),
Student s 才是变量(照图纸盖的房)——和 int 与 int a 一个关系
- 成员可以是任意类型:string、int、double,甚至别的结构体(知识点 4)
- 定义末尾的
}; 分号是高频丢失点,丢了编译器报一串莫名其妙的错
知识点 2:初始化与成员访问(12 分钟)
members.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
};
int main() {
Student s1{"张三", 18, 92.5}; // 方式1:花括号按顺序给全
Student s2; // 方式2:先定义,再逐个赋值
s2.name = "李四";
s2.age = 17;
s2.score = 78;
s1.score += 5; // 成员就是普通变量,随便运算
cout << s1.name << ":" << s1.score << endl; // 张三:97.5
Student s3 = s1; // 整体赋值!一袋子全复印
s3.score = 100;
cout << s1.score << " " << s3.score << endl; // 97.5 100,互不影响
return 0;
}
三个关键认知(板书):
. 读作"的":s1.score = s1 的 score。成员访问运算符优先级很高,s1.score += 5 就是给那个成员加 5
- 整体赋值是结构体的超能力:
s3 = s1 一条语句复制所有成员——第 17 课排序交换两个学生,靠的就是它(平行数组时代要逐条搬)
- 整体赋值是真复制:s3 改 score 不影响 s1,两个独立档案袋
⚠️ 未初始化的成员是垃圾值(Student s2; 后 s2.age 是随机数)——和局部变量一个规矩,先赋值再使用。
知识点 3:结构体指针与 ->(10 分钟)
arrow.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
};
int main() {
Student s{"张三", 18, 92.5};
Student* p = &s; // 指针指向整个档案袋(首成员地址)
cout << p->name << endl; // 张三 ← 箭头访问,最常用
cout << (*p).score << endl; // 92.5 ← 完全等价的笨写法
p->score = 100; // 顺指针改成员
cout << s.score << endl; // 100 ← 改的是原件
return 0;
}
| 写法 | 含义 | 什么时候用 |
s.score | 变量本人.成员 | 手里是结构体变量 |
p->score | 指针->成员 | 手里是结构体指针(推荐) |
(*p).score | 先解引用再.成员 | 理解原理用,括号不能省 |
🚨 *p.score 是错的:. 优先级高于 *,会被解析成 *(p.score)——p 是指针根本没有 .score。要么加括号 (*p).score,要么直接用 p->score。-> 就是为省这对括号发明的。
知识点 4:嵌套与别名(13 分钟)
4.1 结构体套结构体
nest.cpp
#include <iostream>
#include <string>
using namespace std;
struct Date {
int year, month, day;
};
struct Student {
string name;
int age;
double score;
Date birthday; // 成员本身又是一个结构体
};
int main() {
Student s{"张三", 18, 92.5, {2007, 5, 12}};
cout << s.name << " 生日 "
<< s.birthday.year << "-" << s.birthday.month
<< "-" << s.birthday.day << endl; // 张三 生日 2007-5-12
s.birthday.month = 6; // 链式访问:一路 . 下去
return 0;
}
4.2 typedef / using:给类型起短名
alias.cpp
// 老写法(C 语言遗产,读旧代码会见到):
typedef struct {
double x, y;
} Point; // 此后 Point 就是类型名
// 现代 C++ 写法(推荐,和 using namespace 的 using 同款):
using Int = int; // Int 就是 int 的别名
int main() {
Point p{3, 4};
Int n = 10;
return 0;
}
- C++ 里
struct Point {...}; 后直接用 Point 就行,不 typedef 也完全合法——typedef 主要是读 C 旧代码时要认识
sizeof(Point) = 16(两个 double 各 8 字节)——结构体占的内存 ≈ 成员之和(可能有对齐空隙)
综合示例:图书信息卡
book.cpp
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
double price;
};
int main() {
Book b{"三体", "刘慈欣", 59.8};
cout << "《" << b.title << "》 " << b.author
<< " 定价 " << b.price << " 元" << endl;
Book* p = &b;
p->price = 49.9; // 打折,顺指针改原件
cout << "折后 " << b.price << " 元" << endl; // 折后 49.9 元
return 0;
}
✏️ 课堂练习(约 15 分钟)
📄
习题纯文本版:c16.txt(可直接发给学生,不含答案;所有参考答案都在本页绿色折叠框里)
练习 1(必做):我的第一本书
mybook.cpp —— 定义
Book{title, author, price},初始化为你喜欢的一本书,按"《书名》 作者 定价 x 元"格式输出。要求 struct 定义写在 main 外面。
参考代码(先自己做再看)
mybook.cpp
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
double price;
};
int main() {
Book b{"活着", "余华", 35.0};
cout << "《" << b.title << "》 " << b.author
<< " 定价 " << b.price << " 元" << endl;
return 0;
} 输出:《活着》 余华 定价 35 元。别忘了 struct 末尾的分号。
练习 2(必做):指针改档案
ptrstu.cpp ——
Student s{"张三", 18, 92.5}; 用
Student* p = &s; 通过
-> 把年龄改 19、分数加 5,再用
s. 输出验证原件变了(19、97.5)。最后用
(*p).name 输出姓名,体会两种写法等价。
参考代码
ptrstu.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
};
int main() {
Student s{"张三", 18, 92.5};
Student* p = &s;
p->age = 19;
p->score += 5;
cout << s.age << " " << s.score << endl; // 19 97.5
cout << (*p).name << endl; // 张三
return 0;
}
练习 3(选做):带生日的档案
neststu.cpp —— 定义 Date 与含 birthday 成员的 Student,初始化 {"张三", 18, 92.5, {2007, 5, 12}},输出"张三 生日 2007-5-12"。再把月份改成 6 输出一次。
参考代码(先自己做再看)
neststu.cpp
#include <iostream>
#include <string>
using namespace std;
struct Date { int year, month, day; };
struct Student {
string name;
int age;
double score;
Date birthday;
};
int main() {
Student s{"张三", 18, 92.5, {2007, 5, 12}};
cout << s.name << " 生日 " << s.birthday.year
<< "-" << s.birthday.month << "-" << s.birthday.day << endl;
s.birthday.month = 6;
cout << "改后 " << s.birthday.month << " 月" << endl;
return 0;
}
📝 课后作业(下次课前交 .cpp 文件 + 运行截图)
作业 1:坐标点与距离
point.cpp —— 定义
Point{double x, y;},初始化 p{3, 4},用
sqrt(x*x + y*y)(需
#include <cmath>)求到原点距离,输出 5。再输出 sizeof(Point) 看看是不是 16。
参考代码
point.cpp
#include <iostream>
#include <cmath>
using namespace std;
struct Point { double x, y; };
int main() {
Point p{3, 4};
double d = sqrt(p.x * p.x + p.y * p.y);
cout << "距离 = " << d << endl; // 5
cout << "sizeof(Point) = " << sizeof(Point) << endl; // 16
return 0;
}
作业 2:整体赋值复印机
copystu.cpp ——
Student s1{"张三", 18, 92.5}; 用
Student s2 = s1; 整体复制,把 s2 的分数改 100、姓名改"张三副本",输出两个学生验证 s1 毫发无损(92.5、张三)。一句话总结:结构体赋值是复制还是共享?
参考代码(先自己做再看)
copystu.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
};
int main() {
Student s1{"张三", 18, 92.5};
Student s2 = s1; // 一袋子全复印
s2.score = 100;
s2.name = "张三副本";
cout << s1.name << " " << s1.score << endl; // 张三 92.5
cout << s2.name << " " << s2.score << endl; // 张三副本 100
return 0;
} 答案:复制。两个独立档案袋,改谁都不影响另一个——下节课排序交换就靠这个性质。
作业 3:别名与嵌套输出
alias.cpp —— 用
using Stu = Student;(Student 含 Date birthday)给类型起别名,用 Stu 定义变量并初始化生日 {2007, 5, 12},输出"张三 生日 2007-5-12"。说说 using 别名和 #define 宏替换的区别(提示:别名是编译器认的正式类型名)。
参考代码
alias.cpp
#include <iostream>
#include <string>
using namespace std;
struct Date { int year, month, day; };
struct Student {
string name;
int age;
double score;
Date birthday;
};
using Stu = Student; // Stu 就是 Student
int main() {
Stu s{"张三", 18, 92.5, {2007, 5, 12}};
cout << s.name << " 生日 " << s.birthday.year
<< "-" << s.birthday.month << "-" << s.birthday.day << endl;
return 0; // 张三 生日 2007-5-12
} using 别名由编译器处理、有类型检查;#define 只是预处理文本替换,不做检查——起类型别名永远用 using。
作业 4(改错题)
下面的代码有
4 处问题,找出并改正:
有错误的代码
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
}
int main() {
Student s{"张三", 18, 92.5};
Student* p = &s;
cout << p.name << endl;
cout << *p.name << endl;
s.age = "十八";
return 0;
}
参考答案
① struct 定义末尾丢了分号,应为 };——丢了会连累后面整片代码报莫名其妙的错;② p.name:p 是指针不是变量,应写 p->name;③ *p.name:. 优先级高于 *,被解析成 *(p.name) 双重错误——应写 (*p).name 或 p->name;④ s.age = "十八"; 把字符串赋给 int 成员,类型不匹配——改 s.age = 18;。
📌 本课小结(板书用)
- 结构体 = 档案袋:把相关数据打包成新类型,治愈平行数组"下标对齐"的痛点
struct Student {...}; 末尾分号不能丢;类型是图纸,变量才是房
- 初始化按成员顺序
{"张三", 18, 92.5};. 读作"的",成员就是普通变量
- 整体赋值
s2 = s1 是真复制,一袋子全搬、互不影响——排序交换的基石
- 指针访问用
p->成员,等价 (*p).成员;*p.成员 是优先级陷阱
- 嵌套一路 . 下去:
s.birthday.year;using 起别名是编译器认的正式类型名
- 未初始化成员是垃圾值,先赋值再使用
🔭 下节课预告:第 17 课《结构体综合应用》——结构体数组 + 函数 + 枚举三件套合体,做一个真正的学生成绩表:录入、求平均、找最高分、冒泡排序。第三阶段的收官实战!