TestScoresClassDemo.cpp
TestScoresClass.h
/* TestScoreClass.h - Specification file for the TestScoreClass class. */
#ifndef TEST_SCORE_CLASS_H
#define TEST_SCORE_CLASS_H
class TestScore
{
private:
int scoreOne; // Score one
int scoreTwo; // Score two
int scoreThree; // Score three
double average; // Average score
public:
TestScore() // Default constructor
{
scoreOne = 0;
scoreTwo = 0;
scoreThree = 0;
average = 0.0;
}
// Mutators
void setScores(int scO, int scT, int scTh)
{
scoreOne = scO;
scoreTwo = scT;
scoreThree = scTh;
}
void setAverage()
{
average = (scoreOne + scoreTwo + scoreThree) / 3.0;
}
// Accessors
int getScoreOne() const
{ return scoreOne; }
int getScoreTwo() const
{ return scoreTwo; }
int getScoreThree() const
{ return scoreThree; }
double getAverage() const
{ return average; }
};
#endif
TestScoresClassDemo.cpp
/* TestScoreClass.cpp - This program demonstrates the TestScoreClass class. */
#include "TestScoresClass.h"
#include <iomanip>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::setw;
using std::setprecision;
using std::showpoint;
using std::fixed;
void getScores(TestScore &);
void display(const TestScore);
int main()
{
TestScore scores;
cout << "AVERAGE TEST SCORE DEMO\n\n"
<< "Enter three test scores and I will calculate the average for you\n\n";
getScores(scores);
scores.setAverage();
display(scores);
cout << "\n\nThank you for trying this demo! Have a nice day.";
cin.get();
cin.ignore();
return 0;
}
/* **********************************************************
getScores (accepts a reference to a TestScore object)
The user is asked to enter three test scores. The scores
are stored in the appropriate TestScore object member
variables.
********************************************************** */
void getScores(TestScore &scores)
{
int scoreO = 0;
int scoreT = 0;
int scoreTh = 0;
cout << "Score 1: ";
cin >> scoreO;
while (scoreO < 1 || scoreO > 100)
{
cout << "Score 1: ";
cin >> scoreO;
}
cout << "Score 2: ";
cin >> scoreT;
while (scoreT < 1 || scoreT > 100)
{
cout << "Score 2: ";
cin >> scoreT;
}
cout << "Score 3: ";
cin >> scoreTh;
while (scoreTh < 1 || scoreTh > 100)
{
cout << "Score 3: ";
cin >> scoreTh;
}
scores.setScores(scoreO, scoreT, scoreTh);
}
/* **********************************************************
display (accepts a const TestScore object)
This function displays the test scores and the average.
********************************************************** */
void display(const TestScore scores)
{
cout << "\nThese are your test scores:\n\n";
cout << "Score 1: " << scores.getScoreOne() << "\n"
<< "Score 2: " << scores.getScoreTwo() << "\n"
<< "Score 3: " << scores.getScoreThree() << "\n";
cout << fixed << showpoint << setprecision(2);
cout << "\nYou reached an average score of: " << scores.getAverage() << "%";
}
No comments:
Post a Comment