Question
Your job is to implement two member functions in student.h: addGrade(grade) and GPA( ) A main( ) program is provided that creates student objects and
Your job is to implement two member functions in "student.h": addGrade(grade) and GPA( )
A main( ) program is provided that creates student objects and tests the various member functions. The program starts by creating 3 test students: pooja, drago, and sam. Here's the correct output:
pooja (12345): gpa is 2.75 drago (88425): gpa is 0 sam (99999): gpa is 2.2
main.cpp is a read only file
#include
#include "student.h"
using namespace std;
// // testcases // // A couple hard-coded test cases to start. // void testcases() { student pooja("pooja", 12345); pooja.addGrade('A'); pooja.addGrade('B'); pooja.addGrade('A'); pooja.addGrade('F');
cout << pooja.getName() << " (" << pooja.getUIN() << "):" << " gpa is " << pooja.GPA() << endl;
student drago("drago", 88425);
cout << drago.getName() << " (" << drago.getUIN() << "):" << " gpa is " << drago.GPA() << endl;
student grow("grow", 99999); char* originalArray = grow.Grades; grow.addGrade('A'); grow.addGrade('B'); grow.addGrade('A'); grow.addGrade('F'); grow.addGrade('W');
cout << grow.getName() << " (" << grow.getUIN() << "):" << " gpa is " << grow.GPA() << endl;
if (grow.Grades == originalArray) cout << "**Error: addGrade did not properly grow the Grades array?!" << endl; if (grow.N != 5) cout << "**Error: addGrade did not properly increase N?!" << endl; if (grow.ArrayCap != 8) cout << "**Error: addGrade did not properly increase Capacity?!" << endl;
cout << endl; }
// ############################################################## // // main // int main() { string name; int uin; char grade; int count = 0; int cap = 4;
testcases();
cout << "Enter a student's name (one word)> "; cin >> name; cout << "Enter their UIN> "; cin >> uin;
student S(name, uin);
char* originalArray = S.Grades;
cout << "Now enter 0 or more grades, one at a time" << endl; cout << "grade or #> "; cin >> grade;
while (grade != '#') { if (count == cap) originalArray = S.Grades;
S.addGrade(grade); count++;
if (count == (cap+1)) cap = cap * 2;
cout << "grade or #> "; cin >> grade; }
cout << endl;
cout << S.getName() << " (" << S.getUIN() << "):" << " gpa is " << S.GPA() << endl;
// // test that array grew properly if need be: // if (cap > 4) // array had to grow: { if (S.Grades == originalArray) cout << "**Error: addGrade did not properly grow the Grades array?!" << endl; } if (S.N != count) cout << "**Error: addGrade did not properly increase N?!" << endl; if (S.ArrayCap != cap) cout << "**Error: addGrade did not properly increase Capacity?!" << endl;
cout << endl; return 0; }
/*student.h*/
// // student class for keeping track of grades. Uses a dynamically // allocated character array for storing grades. //
#pragma once
#include
using namespace std;
class student { private: string Name; int UIN; //char* Grades; // C-style array of characters //int N; // # of grades in array //int ArrayCap; // total capacity of array
public: // // NOTE: Grades is public *only* for grading purposes, normally // this would be private. Likewise for N and ArrayCap. // char* Grades; // C-style array of characters int N; // # of grades in array int ArrayCap; // total capacity of array
// // constructor: // student(string name, int uin) { Name = name; UIN = uin;
Grades = new char[4]; // empty array of capacity 4: N = 0; ArrayCap = 4; }
// // addGrade: // // Adds another grade to the Grades array, growing dynamically if // necessary (by doubling the capacity). // void addGrade(char grade) { if (N == ArrayCap) // array is full, have to double in size: { // // TODO: // }
// we have space, add to end: Grades[N] = grade; N++; }
// // GPA: // // returns GPA for this student, based on their grades. Returns 0.0 // if the student has no grades. // double GPA() { // // TODO: // return -1; }
// // getName // string getName() { return Name; }
// // getUIN // int getUIN() { return UIN; }
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started