Use this code as a template: #include using namespace std; class Grade{ // }; class GradeBook{ //Note:
Question:
Use this code as a template:
#include
using namespace std;
class Grade{
//
};
class GradeBook{
//Note: assume the length of array is fixed (8).
};
int main(){
int grades[8] = {80,87,92,91,29,68,76,88};
string names[8] = {"a","b","c","d","e","f","g","h"};
//Write your code here
return 0;
}
In this part, you are going to practice Object Oriented Programming (OOP) in C++, such as designing classes.
You are given an array of student names (string) and an array of grades (int). The order of names matches the order of grades, i.e. name[i]'s grade is grade[i]. To make it simple, the number of students is given as a constant. Your task is to create classes to manage the data.
*To make the problem easier, we allow the Grade array in GradeBook to be statically allocated by using constant length. Feel free to use pointer and dynamic allocation as long as the functionality is correct*
The "Grade" class contains the name and grade of a single student. Create a class "Grade" that contains the following:
- aprivate intthat stores grade
- apublic stringthat stores name
- public set methodvoid set_grade(string name, int grade)to set the grade and name of the object
- public get methodint get_grade()to return the grade of the object
The "GradeBook" class contains an array of Grade objects to record student names and their grades. Create a class "GradeBook" that contains the following:
- aprivate Grade arraythat contains Grade objects. The total number of students is fixed, so you can use a static array.
- publicvoid set_value() method to set the value of Grade objects in the array. Parameters are the two arrays given to you that contain names and grades.
- publicvoid print_book()method that prints the entire grade book. It should contain both student name and the corresponding grade. Name and grade for each student should be in one line separated by space.
- publicvoid print_top(int n)method that prints the top n student and their grade. When n exceeds the range of the array, you should simply print all students and grades.
- When printing, you do not need to rank the grades from high to low. Simply printing them without order is fine.
A starter cpp file is attached. In main function, do the following:
- Create a GradeBook object
- Set GradeBook with given arrays
- Print the entire grade book
- Test print_top() method with print_top(3) and print_top(20).
Now, instead of first creating a GradeBook object and setting its value, we use a constructor to initiate the GradeBook with names and grades. Do the following:
- Add a constructor to GradeBook class. The constructor should have two parameters: name array and grade array.
- Comment out your old code that initiates and sets the GradeBook object, and instead use the constructor to initiate it.