重写下列代码中的addStudent function which throw a runtime_error if the number of students exceeds the capacity,并重写出一个符合要求的main函数,原代码如下:#include<iostream> #include"course.h" using namespace std; Course::Course(const string& courseName, int capacity) { numberOfStudents = 0; this->courseName = courseName; this->capacity = capacity; students = new string[capacity]; } Course::~Course(){ delete[]students; } string Course::getCourseName() const { return courseName; } void Course::addStudent(const string& name) { students[numberOfStudents] = name; numberOfStudents++; } string* Course::getStudent()const { return students; } int Course::getNumberOfStudents()const { return numberOfStudents; } int main() { Course course1("Data Structures", 10); Course course2("Database System", 15); course1.addStudent("s1mple"); course1.addStudent("kennys"); course1.addStudent("somedieyoung"); course2.addStudent("niko"); course2.addStudent("pinkfloyd"); cout << "Number of c1 students:" << course1.getNumberOfStudents() << endl; string* students = course1.getStudent(); for (int i = 0; i < course1.getNumberOfStudents(); i++) { cout << students[i] << "," << endl;; } cout << "Number of c2 students:" << course2.getNumberOfStudents() << endl; students = course2.getStudent(); for (int i = 0; i < course2.getNumberOfStudents(); i++) { cout << students[i] << ","; } return 0; }
时间: 2024-01-23 08:02:32 浏览: 127
#include <iostream> #include<iomanip> using namespace std;九九乘法表
重写后的代码如下:
```cpp
#include<iostream>
#include"course.h"
using namespace std;
Course::Course(const string& courseName, int capacity) {
numberOfStudents = 0;
this->courseName = courseName;
this->capacity = capacity;
students = new string[capacity];
}
Course::~Course() {
delete[] students;
}
string Course::getCourseName() const {
return courseName;
}
void Course::addStudent(const string& name) {
if (numberOfStudents >= capacity) {
throw runtime_error("Error: Number of students exceeds capacity!");
}
students[numberOfStudents] = name;
numberOfStudents++;
}
string* Course::getStudent() const {
return students;
}
int Course::getNumberOfStudents() const {
return numberOfStudents;
}
int main() {
Course course1("Data Structures", 3);
Course course2("Database System", 2);
try {
course1.addStudent("s1mple");
course1.addStudent("kennys");
course1.addStudent("somedieyoung");
course1.addStudent("coldzera"); // 超出容量,会抛出异常
}
catch (const runtime_error& e) {
cout << e.what() << endl; // 捕获异常并输出错误信息
}
try {
course2.addStudent("niko");
course2.addStudent("pinkfloyd");
course2.addStudent("getright"); // 超出容量,会抛出异常
}
catch (const runtime_error& e) {
cout << e.what() << endl; // 捕获异常并输出错误信息
}
cout << "Number of c1 students:" << course1.getNumberOfStudents() << endl;
string* students = course1.getStudent();
for (int i = 0; i < course1.getNumberOfStudents(); i++) {
cout << students[i] << "," << endl;;
}
cout << "Number of c2 students:" << course2.getNumberOfStudents() << endl;
students = course2.getStudent();
for (int i = 0; i < course2.getNumberOfStudents(); i++) {
cout << students[i] << "," << endl;;
}
return 0;
}
```
在addStudent函数中加入了判断当前学生数量是否超过容量的语句。如果超过容量,则抛出一个runtime_error异常,并输出错误信息。
在main函数中,对于每个课程都使用try-catch语句捕获可能抛出的异常,并输出错误信息。这样可以保证程序不会因为超出容量而崩溃,而是会输出错误信息并继续运行。
需要注意的是,为了方便测试,我将课程的容量都设置得比较小。在实际使用中,需要根据实际情况来设置合适的容量。
阅读全文