Question

Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...

Write a C++ console program that defines a class named Course that utilizes a dynamically allocated array. Do not use the vector class for this assignment.

The Course class should define private data members for the name of the course, the number of students in the course, an array of student names (string*), and the capacity of the course (the array may not be full of students). Use pointer notation when dealing with the array.

A 2-arg constructor should initialize the course name and also allocate the array given the capacity.

Course(string &courseName, int capacity)

Note that the capacity is the size of the array, but the array may not be full of students.

Define a destructor that will de-allocate the array when a course object is destroyed.

Include accessor methods for the course name and number of students.

Include a mutator method named addStudent that will add a student to the course. A student is represented by a string for the student name. The student name should be added to the array at the appropriate position in the array. If the array's capacity is exceeded then the array's capacity should be increased by 5.

When the array's capacity is increased (inside the addStudent method), print out a short message to the screen:

"Class size has been expanded to " << newCapacity << endl;

A void method should be included that will print out the students in the course.

Test the class from main by creating a course object with an initial capacity of 3. Next add 5 students to the course. Finally display the course name, number of students, and list of students names.

** Note: Do not use square bracket notation [ ] in this program, except for when using the new operator. Use only pointer notation to manipulate the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

class Course{

              private:

              //private attributes

              string name;

              int numStudents;

              string* students;

              int capacity;

             

              public:

              //constructor taking course name and capacity

              Course(string &courseName, int _capacity){

                            name=courseName;

                           capacity=_capacity;

                           //initializing dynamic array

                           students=new string[capacity];

                           numStudents=0;

              }

             

              //destructor

              ~Course(){

                           //deallocating dynamic array. square brackets needs to be used here

                           delete[] students;

              }

             

              //returns the course name

              string getCourseName(){

                           return name;

              }

             

              //returns the number of students

              int getNumberOfStudents(){

                           return numStudents;

              }

             

              //method to add a student to the students array

              void addStudent(string stud_name){

                           //checking if array is full

                           if(numStudents==capacity){

                                         //updating capacity

                                         capacity=capacity+5;

                                         //creating a new dynamic array of new capacity

                                         string* newArray=new string[capacity];

                                         //copying all values from students to new array

                                         for(int i=0;i<numStudents;i++){

                                                       *(newArray+i)=*(students+i);

                                         }

                                         //deleting old array, replacing with new one

                                         delete[] students;

                                         students=newArray;

                                         //displaying a message

                                         cout<<"Class size has been expanded to " << capacity << endl;

                           }

                           //adding new student at the end of the array

                           *(students+numStudents)=stud_name;

                           numStudents++;

              }

             

              //method to print all students

              void printStudents(){

                           cout<<"Following students are opted for this course."<<endl;

                           //looping and printing all students

                           for(int i=0;i<numStudents;i++){

                                         cout<<*(students+i)<<endl;

                           }

              }

                          

};

int main(){

              //creating a course and adding 5 students

              string courseName="CS123";

              Course course(courseName,3);

              course.addStudent("Oliver Queen");

              course.addStudent("Felicity Smoak");

              course.addStudent("John Diggle");

              course.addStudent("Curtis");

              course.addStudent("Dinah");

              //displaying details

              cout<<"Course Name: "<<course.getCourseName()<<endl;

              cout<<"Number of students: "<<course.getNumberOfStudents()<<endl;

              course.printStudents();

              return 0;

}

/*OUTPUT*/

Class size has been expanded to 8

Course Name: CS123

Number of students: 5

Following students are opted for this course.

Oliver Queen

Felicity Smoak

John Diggle

Curtis

Dinah

Add a comment
Know the answer?
Add Answer to:
Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

  • I asked this a little bit ago but didn't clarify exactly what I needed, so I'm...

    I asked this a little bit ago but didn't clarify exactly what I needed, so I'm asking again...I've attached my code below the question. Thanks! Do not change the original contract of the Course class. Instead, ONLY change the implementation of property students, from an array to and ArrayList, and update any methods in class Course that access students to reflect ArrayList. The methods are overridden. That means that you do not need to change the header for methods, only...

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String),...

    Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String), numberOfStudents (int) and courseLecturer (String). ○ A constructor that constructs a Course object with the specified courseName, numberOfStudents and courseLecturer. ○ The relevant get and set methods for the data fields. ○ A toString() method that formats that returns a string that represents a course object in the following format: (courseName, courseLecturer, numberOfStudents) ● Create a new ArrayList called courses1, add 5 courses to...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Assuming the following class declaration: class   Course {        private:               string grade_;        &n

    Assuming the following class declaration: class   Course {        private:               string grade_;               int       units_;         public:              Course ( );              Course ( string grade, int units);               ~Course ( );               string get_grade ( ) const ;               int       get_units ( ) const; }; NOTE: Class definitions are not provided but you may assume they're properly defined. Variable names used in your answers must follow our stated naming convention especially for pointer names. Write a fragment of...

  • In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a tes...

    In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a test grade (integer value) for each student in a class. Validation Tests are graded on a 100 point scale with a 5 point bonus question. So a valid grade should be 0 through 105, inclusive. Processing Your program should work for any...

  • Create a C++ console program that defines a class named EvenNumber that represents an even number....

    Create a C++ console program that defines a class named EvenNumber that represents an even number. Create the class inside a separate header file (.h) and then include this header in your main source code file. The class should have one private integer data field to store the even number. The default constructor should initialize the data field to 0. Also define a 1-arg constructor that initializes the object with the specified value. Define a public getter method named getValue...

  • 2. Write a program named lab3 2.app that contains a class named Student to store student...

    2. Write a program named lab3 2.app that contains a class named Student to store student information Information that will be relevant to store is first name, last name, id number, GPA, and major (1) Define a member function named set0, which allows you to set all member variables. This function has an empty parameter list, and its return type is void. So, within the function you'll prompt for information. Note: For entering major, if you need a space, use...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT