
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Rewrite the addStudent function in C++ so that the Course class in the attached file to throw a runtime_error if the number of students exceeds the capacity. (please include complete written c++ code).thanks
![```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;
}
```
### Explanation:
This C++ code is a basic implementation of a `Course` class, which manages a simple list of students enrolled in a course. Below is an overview of its structure and functionality:
1. **Includes and Namespace:**
- Includes the necessary input/output stream header `<iostream>`.
- Includes a header file `"Course.h"` that likely contains the class declaration.
- Uses the standard namespace with `using namespace std;`.
2. **Constructor (`Course::Course`):**
- Initializes a course with a name and a capacity for students.
- Sets the initial number of students to zero.
- Allocates memory for storing student names based on the course capacity.
3. **Destructor (`Course::~Course`):**
- Frees the allocated memory for the students' list.
4. **Method `getCourseName`:**
- Returns the name of the course.
5. **Method `addStudent`:**
- Adds a student to the course by their name.
- Increments the count of enrolled students.
6. **Method `dropStudent`:**
- Placeholder for removing a student by name; actual implementation is left as an exercise.
7. **Method `getStudents`:**
- Returns a pointer to the array of student names.
8. **Method `getNumberOfStudents`:**
- Returns the current number of enrolled students.
This example demonstrates basic memory management and object-oriented programming concepts in C++.](https://content.bartleby.com/qna-images/question/09af1c01-6e60-4588-9390-2a0cbfc62f20/acfdaf27-c762-4bad-ab65-1dafc2478115/ofy4ta_thumbnail.jpeg)
Transcribed Image Text:```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;
}
```
### Explanation:
This C++ code is a basic implementation of a `Course` class, which manages a simple list of students enrolled in a course. Below is an overview of its structure and functionality:
1. **Includes and Namespace:**
- Includes the necessary input/output stream header `<iostream>`.
- Includes a header file `"Course.h"` that likely contains the class declaration.
- Uses the standard namespace with `using namespace std;`.
2. **Constructor (`Course::Course`):**
- Initializes a course with a name and a capacity for students.
- Sets the initial number of students to zero.
- Allocates memory for storing student names based on the course capacity.
3. **Destructor (`Course::~Course`):**
- Frees the allocated memory for the students' list.
4. **Method `getCourseName`:**
- Returns the name of the course.
5. **Method `addStudent`:**
- Adds a student to the course by their name.
- Increments the count of enrolled students.
6. **Method `dropStudent`:**
- Placeholder for removing a student by name; actual implementation is left as an exercise.
7. **Method `getStudents`:**
- Returns a pointer to the array of student names.
8. **Method `getNumberOfStudents`:**
- Returns the current number of enrolled students.
This example demonstrates basic memory management and object-oriented programming concepts in C++.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 6 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write the full C++ code for main.cpp, mystring.h, and mystring.cpparrow_forwardWhich header file is necessary for C++ OOP? What attribute permits open recursion?arrow_forwardWorking on an assignment for C++ codding. Need a little help. I need to write a function that takes in the user's input and returns true if the input contains "polly", "cracker", or "hello". Thanks in advance!arrow_forward
- void change_current_directory(char *directory) {} This is to be done in Carrow_forwardIn C++, can I get a code example for a function that will return the intersection items for two sets. It will return/print out the shared (intersection) items for them. I am looking for an actual function preferably for a set class, comparing one instance of a set class with another, but definitely NOT a STL keyword. Thank you.arrow_forwardDateType keeps only the integer representation of the month,day, and year. When a month is wanted in string form, the string iscalculated. An alternate approach would be to add a string field tothe date and calculate and store the string representation in theInitialize function. Which methods would have to bechanged? Would this change make the use of an if statement tofind the appropriate string more or less attractive? Write the code in C++for this if statement.arrow_forward
- Write a C++ Program using classes, functions (recursive and otherwise), arrays and other C++ commands to sort a file using Selection Sort. The user will enter the filename. The file should be sorted by the following fields: first by Last Name, then by Mother’s Maiden Name, then by First Name and finally by Second Name (or Initial). The sorted file should be written to file and the first 10 results displayed on screen. Use the following format for both the screen output and the file created: ----------------------------------------------------------------| Last Name | Mother’s Maiden Name | First Name | Second Name | ----------------------------------------------------------------| Saotome | Tendo | Ranma | P | |---------------------------------------------------------------| Son | Kakarot | Goku | A |----------------------------------------------------------------| Yeager | Kaiser | Eren…arrow_forwardIn C++, what is the difference between emplace_back and push_back, when using vectors? I looked at other sources, and still find it confusing as to what s the difference between the two of these.arrow_forwardI need help with this assignment, I need to make a struct to implement a limited Python style list in C++. When you bootstrap the exercise, you will be given a main.cpp file, with a simple program written in it. The program declares a Python style list and appends several elements to it, before printing it out to the terminal. The file is where I would setup my code #ifndef PY_LIST_H #define PY_LIST_H #include <iostream> struct PyList { // Declare your vars here PyList(){ // Implement this } void append(int x){ // Implement this } void append(char x){ // Implement this } void append(float x){ // Implement this } ~PyList(){ // Implement this } }; std::ostream& operator<<(std::ostream& os, const PyList& pyList){ os << '['; // Complete this implementation os << ']'; return os; } #endif Here is the main.cpp file that it must be able to run and the float must not have floating zeros #include <iostream> #include "PyList.h"…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education