练习1:使用公有继承设计学生信息类¶
任务描述¶
设计人员类 People 和学生类 Student。Student 使用公有继承复用 People 的姓名功能,并在不同头文件中分别声明两个类。
姓名和学号均应为私有数据成员;测试代码通过设置器为对象赋值,再调用输出函数验证结果。
相关知识¶
公有继承¶
公有继承表达“是一种”(is-a)关系。学生是一种人员,因此可以使用如下语法:
基类的 public 成员在派生类中仍保持 public 访问性。派生类的成员函数和派生类对象都能调用基类的公有成员函数。
私有数据与访问器、设置器¶
数据成员设为 private 可防止类外代码直接修改对象状态。类通过公有设置器(setter)和访问器(getter)提供受控访问:
class People {
private:
std::string name;
public:
void setName(const std::string& name);
const std::string& getName() const;
};
const 访问器只读取对象状态,不会修改数据成员。
多文件组织¶
本题将两个类分别声明在不同头文件中:
People.h // People 类声明
Student.h // 包含 People.h,并声明 Student 类
People.cpp // People 成员函数实现
Student.cpp // Student 成员函数实现
main.cpp // 测试代码
Student.h 需要包含 People.h,因为 Student 的定义依赖其基类定义。
编程要求¶
- 使用 C++11 标准编写程序。
- 在
People.h中声明People类。姓名成员必须为私有的std::string name,并提供:
- 在
Student.h中包含People.h,并声明class Student : public People。学号成员必须为私有的int sid,并提供:
- 在
People.cpp和Student.cpp中分别实现对应成员函数。 printSid输出学号:学号值;printName输出姓名:姓名值。- 在
test函数中,仅使用setSid和继承得到的setName为对象赋值;使用assert验证访问器返回值和输出内容。
待完成代码¶
People.h¶
Student.h¶
#ifndef STUDENT_H
#define STUDENT_H
#include "People.h"
// TODO:使用 public 继承声明 Student 类,学号成员必须为 private
#endif
People.cpp¶
Student.cpp¶
main.cpp¶
#include "Student.h"
#include <cassert>
#include <iostream>
#include <sstream>
void test() {
Student student;
student.setSid(1);
student.setName("张大民");
assert(student.getSid() == 1);
assert(student.getName() == "张大民");
std::ostringstream output;
std::streambuf* oldBuffer = std::cout.rdbuf(output.rdbuf());
student.printSid();
student.printName();
std::cout.rdbuf(oldBuffer);
assert(output.str() == "学号:1\n姓名:张大民\n");
}
int main() {
test();
std::cout << "本关测试通过" << std::endl;
return 0;
}
测试说明¶
测试通过设置器将学生学号设置为 1、姓名设置为 张大民。预期输出为:
可使用以下命令编译源文件:
开始你的任务吧,祝你成功!