In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the book does not have to be in alphabetical order), and to list all student records (name and associated grades) it contains. To add a student to the end of the book, the user should not have to specify a position, (ie. your GradeBook should keep track of the last position and add to the end automatically). Your Student class should include at least the operations to allow entry of the last name and up to 5 grades, and to return the name and grades for that student.
You should write a main program that creates a grade book and presents a menu to the user that allows them to select either Add (A), or List (L), or Quit (Q). Add should allow the user to enter a student record (name and grades) and add it to the end of the Gradebook list. List should list all student records currently in the grade book. You should be able to add and list repeatedly, until you select Q to quit.
Use good coding style and principles for all code and input/output formatting. All data in a class must be private. Put each class declaration in its own header file and its implementation in a separate .cpp file.
Helpful hints on how to approach this lab
1. Using the Gradebook object, call the function in the Gradebook class to add a student, passing in the name of the student that the user entered.
------Student Class - Student.h
#ifndef STUDENT_H_
#define STUDENT_H_
#include <iostream.h>
#include <cstring.h>
#define MAX_GRADES 5
using namespace std;
/**
* Student class
*/
class Student {
private:
string studentLastName; //last name of string
int grades[MAX_GRADES]; //grades of the student
int numGrades; //number of grades given
public:
Student();
const int getGrade(int numGrade) const;
void setGrade(int grade);
const string& getStudentLastName() const;
void setStudentLastName(const string& studentLastName);
int getNumGrades() const;
};
#endif /* STUDENT_H_ */
-------Gradebook Class - Gradebook.h
#ifndef GRADEBOOK_H_
#define GRADEBOOK_H_
#include <iostream>
#include "Student.h"
#define MAX_STUDENTS 3
using namespace std;
/**
* GradeBook class
*/
class GradeBook {
private:
Student students[MAX_STUDENTS]; //students in grade book
int numStudents; //number of students in the grade book
public:
GradeBook();
void addStudent(Student student);
void listStudents();
};
#endif /* GRADEBOOK_H_ */
--------Student.cpp
#include "Student.h"
/**
* Constructor
*/
Student::Student() {
studentLastName="";
numGrades=0;
}
/**
* returns grade of a specified number
*/
const int Student::getGrade(int numGrade) const {
if(numGrade >= 0 && numGrade <= MAX_GRADES)
return grades[numGrade];
else
{
cerr<<"Can not return grade. Wrong input."<<endl;
return -1;
}
}
/**
* sets a given grade
*/
void Student::setGrade(int grade)
{
if(grade > 0 && grade <=100)
{
if(numGrades == MAX_GRADES)
{
cerr<<"Cannot add the grade. Max. grades already alloted."<<endl;
return;
}
grades[numGrades]=grade;
numGrades++;
}
}
/**
* returns student last name
*/
const string& Student::getStudentLastName() const {
return studentLastName;
}
/**
* sets student last name
*/
void Student::setStudentLastName(const string& studentLastName) {
this->studentLastName = studentLastName;
}
/**
* returns number of grades alloted to the student
*/
int Student::getNumGrades() const {
return numGrades;
}
------Gradebook.cpp
#include "GradeBook.h"
/**
* constructor
*/
GradeBook::GradeBook() {
numStudents=0;
}
void GradeBook::addStudent(Student student) {
if(numStudents==MAX_STUDENTS)
{
cerr<<"Cannot add the student. Max. students already alloted."<<endl;
return;
}
students[numStudents]=student;
numStudents++;
}
void GradeBook::listStudents() {
if(numStudents==0)
{
cerr<<"No student in grade book as of now."<<endl;
return;
}
for(int i=0;i<numStudents;i++)
{
Student student=students[i];
cout<<"Student record "<<(i+1)<<":"<<endl;
cout<<"Last name:"<<student.getStudentLastName()<<endl;
for(int j=0;j<student.getNumGrades();j++)
{
cout<<"Grade "<<(j+1)<<":"<<student.getGrade(j)<<endl;
}
}
}
-----gradebookdemo.cpp
#include <iostream.h>
#include <cstdlib.h>
#include <cstdio.h>
#include "GradeBook.h"
using namespace std;
/**
* main function - demo of GradeBook
*/
int main() {
GradeBook gradebook; //create object of grade book
char choice; //takes users choice
string name; //takes name of student given by user
int grade, numGrades; //grade of student given by user
Student* student; // student to be added to grade book
//create a loop to read 3 students data
while(1)
{
fflush(stdin);
//list of options
cout<<"List of Options:"<<endl;
cout<<"Add(A):"<<endl;
cout<<"List(L):"<<endl;
cout<<"Quit(Q):"<<endl;
cout<<"Choice:";
cin>>choice;
//Process the requirement of user based on choice
switch(choice)
{
case 'A':
student = new Student;
cout<<"Enter last name:";
cin>>name;
student->setStudentLastName(name);
cout<<"How many grades (Max 10)?";
cin>>numGrades;
cout<<"Enter grades:";
for(int i=0;i<numGrades;i++)
{
cout<<"Enter grade "<<(i+1)<<":";
cin>>grade;
student->setGrade(grade);
}
gradebook.addStudent(*student);
break;
case 'L':
gradebook.listStudents();
break;
case 'Q':
cout<<"Thank you! Quitting..."<<endl;
exit(1);
default:
cerr<<"Wrong choice! Re-enter."<<endl;
break;
}
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.