TestScores.h
#ifndef TEST_SCORES_H_
#define TEST_SCORES_H_
class TestScores
{
private:
int *scorePtr;
int arraySize;
int averageScore;
public:
// Exception class
class OutOfRange
{
private:
int errScore;
public:
OutOfRange(int subscript)
{
errScore = subscript;
}
int getErrSubscript() const
{ return errScore; }
};
TestScores(int);
TestScores(const TestScores &);
~TestScores();
// Accessor function
void calcAverage();
// Mutator function
int getAverageScore() const
{ return averageScore; }
// Overloaded operator functions
const TestScores operator = (const TestScores &);
int &operator [](const int &sub);
};
#endif
TestScores.cpp
#include "TestScores.h"
/* **********************************************************
TestScores::TestScores() - Constructor
Sets the size of the array and allocates memory for it.
********************************************************** */
TestScores::TestScores(int size)
{
averageScore = 0;
arraySize = size;
scorePtr = new int[size];
for (int count = 0; count < arraySize; count++)
{
*(scorePtr + count) = 0;
}
}
/* **********************************************************
TestScores::TestScores() - Copy Constructor
********************************************************** */
TestScores::TestScores(const TestScores &obj)
{
arraySize = obj.arraySize;
scorePtr = new int[arraySize];
for (int count = 0; count < arraySize; count++)
{
*(scorePtr + count) = *(obj.scorePtr + count);
}
}
/* **********************************************************
TestScores::~TestScores() - Destructor
********************************************************** */
TestScores::~TestScores()
{
if (arraySize > 0)
{
delete [] scorePtr;
}
}
/* **********************************************************
TestScores::operator =() : const obj &
This function performs a self-assignment check.
********************************************************** */
const TestScores TestScores::operator = (const TestScores &right)
{
if (this != &right)
{
delete [] scorePtr;
arraySize = right.arraySize;
scorePtr = new int[arraySize];
for (int count = 0; count < arraySize; count++)
{
*(scorePtr + count) = *(right.scorePtr + count);
}
}
return *this;
}
/* **********************************************************
TestScores::calcAverage()
This function calculates the average of all test scores.
If a score is lower 0 or greater 100, the OutOfRange
exception is thrown. Otherwise the score is accumulated,
and the average score is calculated, before the function
exits.
********************************************************** */
void TestScores::calcAverage()
{
int sumTotal = 0;
for (int count = 0; count < arraySize; count++)
{
if (*(scorePtr + count) < 0 || *(scorePtr + count) > 100)
{
throw OutOfRange(count);
}
else
{
sumTotal += *(scorePtr + count);
}
}
averageScore = (sumTotal / arraySize);
}
/* **********************************************************
TestScores::operator []() : const int &
Returns a reference to the element in the array indexed by
the subscript.
********************************************************** */
int &TestScores::operator [](const int &sub)
{
return scorePtr[sub];
}
TestScoresDm.cpp
#include "TestScores.h"
#include <iostream>
using std::cin;
using std::cout;
#include <string>
using std::string;
/* **********************************************************
Function Definition
********************************************************** */
void evalScores(TestScores &, const int);
int main()
{
const int SIZE = 5;
const int NUM_STUDENTS = 3;
int score = 0;
const string studentNames[NUM_STUDENTS]{ "M. Hiroshi", "A. Ogawa", "M. Takahata" };
TestScores scoreArr(SIZE);
cout << "YAMAGATA 4th JUNIOR HIGH SCHOOL - AVERAGE TEST SCORE CALCULATOR\n\n";
for (int index = 0; index < NUM_STUDENTS; index++)
{
cout << "Student Name: " << studentNames[index] << "\n\n";
for (int count = 0; count < SIZE; count++)
{
cout << "Test-Score #" << (count + 1) << ": ";
cin >> scoreArr[count];
}
evalScores(scoreArr, SIZE);
cout << "\nThe average score of this student is: "
<< scoreArr.getAverageScore() << "\n";
}
cout << "\n";
cin.ignore();
cin.get();
return 0;
}
/* **********************************************************
evalScores() : obj &, const int
When the try/catch block in this function executes, the
object's calcAverage() function is called. If all scores
are found to be valid, the function exits. Otherwise the
user is repeatedly asked to input a valid value until one
is entered. This new value is assigned to the appropriate
array subscript position.
********************************************************** */
void evalScores(TestScores &scoreArr, const int size)
{
bool tryAgain = true;
while (tryAgain)
{
try
{
scoreArr.calcAverage();
tryAgain = false;
}
catch (TestScores::OutOfRange score)
{
cout << "\nInvalid Score: " << scoreArr[score.getErrSubscript()] << "\n";
cout << "Enter a valid score: ";
cin >> scoreArr[score.getErrSubscript()];
}
}
}
No comments:
Post a Comment