重写程序清单11-16中Course类的addStudent函数c++,使得Course.cpp中,如果学生的数量超过了容量,代码抛出runtime_error异常
时间: 2024-03-14 11:43:55 浏览: 94
以下是重写后的addStudent函数代码:
```cpp
void Course::addStudent(const string& name) {
if (numberOfStudents >= capacity) {
throw runtime_error("Exceeding the capacity!");
}
students[numberOfStudents] = name;
++numberOfStudents;
}
```
在该代码中,我们首先检查学生的数量是否超过了容量。如果是,则抛出一个runtime_error异常。否则,将学生添加到students数组中,并将numberOfStudents加1。
相关问题
(使得Course类可复制)重写程序清单10-6中的Course类,增加一个clone 方法,执行students 域上的深度复制。
以下是修改后的Course类,增加了clone方法来实现students域的深拷贝:
```java
public class Course implements Cloneable {
private String courseName;
private ArrayList<String> students = new ArrayList<>();
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students.add(student);
}
public String[] getStudents() {
return students.toArray(new String[0]);
}
public int getNumberOfStudents() {
return students.size();
}
public String getCourseName() {
return courseName;
}
@Override
public Object clone() {
try {
Course cloned = (Course) super.clone();
cloned.students = (ArrayList<String>) students.clone();
return cloned;
} catch (CloneNotSupportedException e) {
return null;
}
}
}
```
在clone方法中,首先调用了父类的clone方法,得到一个浅拷贝的Course对象,然后将students域进行深拷贝,最后返回新的Course对象。这样,当我们使用clone方法复制一个Course对象时,得到的新对象将会拥有一个独立的、深拷贝的students列表,与原对象的students列表完全独立。
重写下列代码中的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; }
重写后的代码如下:
```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语句捕获可能抛出的异常,并输出错误信息。这样可以保证程序不会因为超出容量而崩溃,而是会输出错误信息并继续运行。
需要注意的是,为了方便测试,我将课程的容量都设置得比较小。在实际使用中,需要根据实际情况来设置合适的容量。
阅读全文