Classes and Objects Assignment in C++

Define a class called Class (uppercase C), with title (string), units (int) and grade (char) for class title, number of units and grade received by student, respectively.

 

Include the following member functions:

 

Default constructor that initializes title to a NULL string, units to 0 and grade to ‘-‘.

A function that returns the class units (get_units) and a function that returns the grade (get_grade)

Destructor which may have an empty body.

Overload the insertion operator (>>) and extraction operation (<<) for Class to read and print Class data.

Define a class called Student, with name (string), gpa (double), numOfClasses and classPtr (Class pointer) for student name, GPA, number of classes taken and array of classes taken by the student, respectively.

Include the following member functions:

 

Default constructor that initializes name to a NULL string, gpa to 0.0, numOfClasses to 0 and classPtr to NULL (or 0).

A function called read() to read student data from the user. The function must first read Student data and then use the overloaded insertion operator for Class to read the information for each class taken.

A function called print() to print student information on the screen. The function must first print Student data and then use the overloaded extraction operator for Class to print the information for each class taken.

A void function called calc_gpa() that calculates the student GPA from classes taken and assigns it to student’s gpa

A function called get_gpa() that returns gpa to the caller

 

Destructor function to deallocate dynamically allocated memory for all classes belonging to Student

To calculate the GPA, the function must multiply each class units with the points received for the class and then add all these products together and then divide the sum of the products by the total number of units for all classes taken. A grade of 'A' is worth 4 points, 'B' is worth 3 points, 'C' is 2 points, 'D' is 1 and 'F' is worth 0 points.

For example, if the student took the following classes:

Class 1: 4 units, letter grade of ‘B’, points = 4 x 3

Class 2: 3 units, letter grade of ‘C’, points = 3 x 2

Class 3: 3 units, letter grade of ‘A’, points = 3 x 4

Class 4: 3 units, letter grade of ‘B’, points = 3 x 3

GPA = (12 + 6 + 12 + 9) / (4 + 3 + 3 + 3) = 3.0

 

Publicly derive a class called StudentWorker from Student. In addition to the inherited data, StudentWorker must also contain weeklyHours (int) and hourlyRate (double) for weekly hours and hourly rate, respectively.

Include the following member functions:

 

Default constructor that uses Student class constructor to first initialize its inherited data and then set weeklyHours to 0 and hourlyRate to 0.0

A function called read() that uses Student class read() function to first read inherited data from the user and then its additional data (weeklyHours and hourlyRate)

A function called print() that uses Student class print() function to first print inherited data on the screen and then its additional data (weeklyHours and hourlyRate)

Destructor function to deallocate dynamically allocated memory

Note that the reason Student and StudentWorker have different read() and print() functions is because the latter has additional data. But, the read() and print() function of StudentWorker use the read() and print() functions of Student class to avoid repeating code already written for Student class. This is key to code reuse in OOP and why we do inheritance.

Write the main program where you:

 

Declare an array of 70 Student pointers, all initialized to NULL (0).

Continuously, ask the user if he or she wants to enter a student. If the answer is ‘y’ or ‘Y’ (for yes), ask if the student is a student worker. If the answer is ‘y’ or ‘Y’, dynamically allocate a new StudentWorker; if not, allocate memory for a new Student. As each Student or StudentWorker is instantiated, regardless of whether it’s a Student object or StudentWorker object, save the returned pointer in the array of Student pointers, and then use that pointer to call the appropriate read() function to read the data for either Student or StudentWorker, and the print() function to print the data for either type of object, as the case may be. Continue until the user decides not to continue.

If you have declared the read and print functions in Student class correctly, you will be able to use the same Student pointer to call the read() and print() functions of Student class for Student objects and the read() and print() functions of StudentWorker class for StudentWorker objects, due to polymorphism, which delays the binding between the object and the function until run time, rather than compile time, so the correct function gets called. This simplifies the code and is key to polymorphism.

Once all students are read from the user and their pointers are stored in the array of Student pointers, print them all (student name, gpa and classes taken, including class title, units and grade received). For StudentWorkers, the printed data must also include weekly hours and hourly rate.

Then, take the average of all the GPA’s and print it.

 

Finally, delete all dynamically allocated Students and StudentWorkers using the array of Student pointers.

 

Need a custom answer at your budget?

This assignment has been answered 5 times in private sessions.

© 2021 Codify Tutor. All rights reserved