using c++. please show your output screen. Consider the class Course given in th
ID: 3722484 • Letter: U
Question
using c++. please show your output screen.
Consider the class Course given in the appendix (last page in this file). Rewrite the addStudent function in the Course.cpp to throw a runtime_error if the number of students exceeds the capacity.
#ifndef COURSE_H
#define COURSE_H
#include <string>
using namespace std;
class Course
{
public:
Course(const string& courseName, int capacity);
~Course();
string getCourseName() const;
void addStudent(const string& name);
void dropStudent(const string& name);
string* getStudents() const;
int getNumberOfStudents() const;
private:
string courseName;
string* students;
int numberOfStudents;
int capacity;
};
#endif
StackOfIntegers.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)
{
students[numberOfStudents] = name;
numberOfStudents++;
}
void Course::dropStudent(const string& name)
{
// Left as an exercise
}
string* Course::getStudents() const
{
return students;
}
int Course::getNumberOfStudents() const
{
return numberOfStudents;
}
using c++. please show your output screen.
Consider the class Course given in the appendix (last page in this file). Rewrite the addStudent function in the Course.cpp to throw a runtime_error if the number of students exceeds the capacity.
#ifndef COURSE_H
#define COURSE_H
#include <string>
using namespace std;
class Course
{
public:
Course(const string& courseName, int capacity);
~Course();
string getCourseName() const;
void addStudent(const string& name);
void dropStudent(const string& name);
string* getStudents() const;
int getNumberOfStudents() const;
private:
string courseName;
string* students;
int numberOfStudents;
int capacity;
};
#endif
Explanation / Answer
Please replace the code inside addStudent with -
if (numberOfStudents >= capacity){
throw runtime_error("Error: Number of Students greater than capacity!");
} else {
students[numberOfStudents] = name;
numberOfStudents++;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.