GradedActivity.h
#ifndef GRADED_ACTIVITY_H_
#define GRADED_ACTIVITY_H_
class GradedActivity
{
protected:
double score; // To hold the numeric score
public:
// Default constructor
GradedActivity()
{ score = 0.0; }
// Constructor
GradedActivity(double s)
{ score = s; }
// Mutator function
void setScore(double s)
{ score = s; }
// Accessor function
double getScore() const
{ return score; }
virtual char getLetterGrade() const;
};
#endif
GradedActivity.cpp
#include "GradedActivity.h"
/* **********************************************************
Member function GradedActivity::getLetterGrade()
********************************************************** */
char GradedActivity::getLetterGrade() const
{
char letterGrade; // To hold the letter grade
if (score > 89)
{
letterGrade = 'A';
}
else if (score > 79)
{
letterGrade = 'B';
}
else if (score > 69)
{
letterGrade = 'C';
}
else if (score > 59)
{
letterGrade = 'D';
}
else
{
letterGrade = 'F';
}
return letterGrade;
}
PassFailActivity.h
#ifndef PASS_FAIL_ACTIVITY_H_
#define PASS_FAIL_ACTIVITY_H_
#include "GradedActivity.h"
class PassFailActivity : public GradedActivity
{
protected:
double minPassingScore; // Minimum passing score
public:
PassFailActivity() // Default constructor
{ minPassingScore = 0.0; }
PassFailActivity(double mps) : GradedActivity()
{ minPassingScore = mps; }
// Mutator
void setMinPassingScore(double mps)
{ minPassingScore = mps; }
// Accessors
double getMinPassingScore() const
{ return minPassingScore; }
virtual char getLetterGrade() const;
};
#endif
PassFailActivity.cpp
#include "PassFailActivity.h"
/* **********************************************************
PassFailActivity::getLetterGrade()
This function returns 'P' if the score is passing,
otherwise it returns 'F'.
********************************************************** */
char PassFailActivity::getLetterGrade() const
{
char letterGrade;
if (score >= minPassingScore)
{
letterGrade = 'P';
}
else
{
letterGrade = 'F';
}
return letterGrade;
}
PassFailExam.h
#ifndef PASS_FAIL_EXAM_H_
#define PASS_FAIL_EXAM_H_
#include "PassFailActivity.h"
class PassFailExam : public PassFailActivity
{
private:
int numQuestions; // Number of questions
double pointsEach; // Points for each question
int numMissed; // Number of questions missed
public:
// Default constructor
PassFailExam()
{
numQuestions = 0;
pointsEach = 0.0;
numMissed = 0;
}
// Constructor
PassFailExam(int questions, int missed, double mps) : PassFailActivity(mps)
{
set(questions, missed);
}
// Mutator function
void set(int, int); // Defined in PassFailExam.cpp
// Accessor functions
int getNumQuestions() const
{ return numQuestions; }
double getPointsEach() const
{ return pointsEach; }
int getNumMissed() const
{ return numMissed; }
};
#endif
PassFailExam.cpp
#include "PassFailExam.h"
/* **********************************************************
PassFailExam::set() : int, int
The parameters are the number of questions and the number
of questions missed.
********************************************************** */
void PassFailExam::set(int questions, int missed)
{
double numericScore; // To hold the numeric score
// Set the number of questions and number missed
numQuestions = questions;
numMissed = missed;
// Calculate the points for each questions
pointsEach = 100.0 / numQuestions;
// Calculate the numeric score for this exam
numericScore = 100.0 - (missed * pointsEach);
// Call the inherited setScore function to set the numeric score
setScore(numericScore);
}
Essay.h
#ifndef ESSAY_H_
#define ESSAY_H_
#include "GradedActivity.h"
// Max amount of achievable points
const int GRAMMAR = 30;
const int SPELLING = 20;
const int CORRECT_LENGTH = 20;
const int CONTENT = 30;
// Essay class derived from the GradedActivity class
class Essay : public GradedActivity
{
private:
double essayScore; // The score a student has achieved
int grammar; // Grammar score
int spelling; // Spelling score
int length; // Correct length score
int content; // Content score
public:
// Constructor
Essay()
{
essayScore = 0.0;
grammar = 0;
spelling = 0;
length = 0;
content = 0;
}
// Parameterized constructor
Essay(int gr, int sp, int len, int cont)
{
set(gr, sp, len, cont);
}
// Mutator functions
void set(int, int, int, int);
void addScore();
// Accessor functions
int getGrammarScore() const
{ return grammar; }
int getSpellingscore() const
{ return spelling; }
int getCorrectLengthScore() const
{ return length; }
int getContentScore() const
{ return content; }
};
#endif
Essay.cpp
#include "Essay.h"
/* **********************************************************
Essay::set() : int, int, int, int
This function assigns the scores achieved by a student to
the appropriate member variables.
********************************************************** */
void Essay::set(int gr, int sp, int len, int cont)
{
grammar = gr;
spelling = sp;
length = len;
content = cont;
addScore();
}
/* **********************************************************
Essay::addScore()
This function calculates the total score achieved, then
calls the inherited setScore() function to set the numeric
score.
********************************************************** */
void Essay::addScore()
{
essayScore = grammar + spelling + length + content;
setScore(essayScore);
}
FinalExam.h
#ifndef FINAL_EXAM_H_
#define FINAL_EXAM_H_
#include "GradedActivity.h"
class FinalExam : public GradedActivity
{
private:
int numQuestions; // Number of questions
double pointsEach; // Points for each question
int numMissed; // Number of questions missed
public:
// Default constructor
FinalExam()
{
numQuestions = 0;
pointsEach = 0.0;
numMissed = 0;
}
// Constructor
FinalExam(int questions, int missed)
{
set(questions, missed);
}
// Mutator function
void set(int, int); // Defined in FinalExam.cpp
void adjustScore(); // Defined in FinalExam.cpp
// Accessor functions
double getNumQuestions() const
{ return numQuestions; }
double getPointsEach() const
{ return pointsEach; }
int getNumMissed() const
{ return numMissed; }
};
#endif
FinalExam.cpp
#include "FinalExam.h"
/* **********************************************************
FinalExam::set() : int, int
The parameters are the number of questions and the number
of questions missed.
********************************************************** */
void FinalExam::set(int questions, int missed)
{
double numericScore; // To hold the numeric score
// Set the number of questions and number missed
numQuestions = questions;
numMissed = missed;
// Calculate the points for each question
pointsEach = 100.0 / numQuestions;
// Calculate the numeric score for this exam
numericScore = 100.0 - (missed * pointsEach);
// Call the inherited setScore function to set the numeric score
setScore(numericScore);
// Call the adjustScore function to adjust the score
adjustScore();
}
/* **********************************************************
FinalExam::adjustScore()
If score is within 0.5 points of the next whole point, it
rounds the score up and recalculates the letter grade.
********************************************************** */
void FinalExam::adjustScore()
{
double fraction = score - static_cast<int>(score);
if (fraction >= 0.5)
{
// Adjust the score variable in the GradedActivity class
score += (1.0 - fraction);
}
}
CourseGrades.h
#ifndef COURSE_GRADES_H_
#define COURSE_GRADES_H_
#include "GradedActivity.h"
#include "PassFailExam.h"
#include "FinalExam.h"
#include "Essay.h"
#include <array>
const int NUM_OBJ = 4;
class CourseGrades
{
private:
std::array<GradedActivity *, NUM_OBJ> grades; // Array of GradedActivity pointers
public:
// Constructor
CourseGrades() : grades{}
{
}
// Mutator functions
void setLab(GradedActivity *);
void setPassFailExam(PassFailExam *);
void setEssay(Essay *);
void setFinalExam(FinalExam *);
// Accessor function
void print() const;
};
#endif
CourseGrades.cpp
#include "CourseGrades.h"
#include <iostream>
using std::cout;
#include <string>
using std::string;
/* **********************************************************
CourseGrades::setLab() : obj *
Assigns a GradedActivity object to element 0 of the grades
array.
********************************************************** */
void CourseGrades::setLab(GradedActivity *gradedActivity)
{
grades[0] = gradedActivity;
}
/* **********************************************************
CourseGrades::setPassFailExam() : obj *
Assigns a PassFailExam object to element 1 of the grades
array.
********************************************************** */
void CourseGrades::setPassFailExam(PassFailExam *passFailExam)
{
grades[1] = passFailExam;
}
/* **********************************************************
CourseGrades::setEssay() : obj *
Assigns a CourseGrades object to element 2 of the grades
array.
********************************************************** */
void CourseGrades::setEssay(Essay *essay)
{
grades[2] = essay;
}
/* **********************************************************
CourseGrades::setFinalExam() : obj *
Assigns a FinalExam object to element 0 of the grades
array.
********************************************************** */
void CourseGrades::setFinalExam(FinalExam *finalExam)
{
grades[3] = finalExam;
}
/* **********************************************************
CourseGrades::print()
The print function outputs the activity name and grade
data for each element in the grades array.
********************************************************** */
void CourseGrades::print() const
{
std::array<string, NUM_OBJ> activity { "LAB ACTIVITY", "PASS FAIL EXAM",
"ESSAY", "FINAL EXAM" };
for (size_t i = 0; i < NUM_OBJ; i++)
{
cout << activity[i] << "\n";
cout << "Score: " << grades[i]->getScore() << "\n";
cout << "Letter Grade: " << grades[i]->getLetterGrade() << "\n\n";
}
}
CourseGradesDm.cpp
#include "CourseGrades.h"
#include <iostream>
using std::cin;
using std::cout;
int main()
{
// Create a course grades object
CourseGrades grades;
// Create a dynamically allocated GradedActivity object
GradedActivity *gradedActivity = new GradedActivity(89);
grades.setLab(gradedActivity);
// Create a dynamically allocated PassFailExam object
PassFailExam *passFailExam = new PassFailExam(10, 3, 70.0);
grades.setPassFailExam(passFailExam);
// Create a dynamically allocated Essay object
Essay *essay = new Essay(25, 18, 14, 11);
grades.setEssay(essay);
// Create a dynamically allocated FinalExam object
FinalExam *finalExam = new FinalExam(50, 13);
grades.setFinalExam(finalExam);
cout << "YAMAGATA 4th JUNIOR HIGH SCHOOL - GRADE SUMMARY\n\n";
cout << "Student Name: Aikawa, Ryoji\n\n";
grades.print();
// Free the memory
delete finalExam;
delete essay;
delete passFailExam;
delete gradedActivity;
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment