根据任务契约审查这个生成的 Course 类。
实现一个初始为空、容量必须为正、满员时拒绝注册,并提供只读学生访问接口的课程类。
C++
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
class Course {
public:
Course(std::string name, std::size_t capacity)
: enrolled_(capacity), capacity_(capacity), name_(std::move(name)) {}
void enroll(std::string student) {
students_.push_back(std::move(student));
++enrolled_;
}
std::vector<std::string> students() const { return students_; }
std::size_t remaining() const { return capacity_ - enrolled_; }
private:
std::size_t enrolled_;
std::size_t capacity_;
std::string name_;
std::vector<std::string> students_;
};
生成代码仅作示例,不代表任何特定模型