I need help with this C++ assignment. It consists of using Inheritance (base cla
ID: 3858153 • Letter: I
Question
I need help with this C++ assignment.
It consists of using Inheritance (base classes and derived classes) and input and output files. Here are the instructions:
Task You will design a set of classes for storing student information, along with a main program that will read student information from a file, store the data, compute final grades, and then print a summary report to an output file. Details 1. Design a set of classes that store student grade information. There should be one base class to store common data, and three derived classes that divide the set of students into three categories: English students, History students, and Math students. All data stored in these classes should be private or protected. Any access to class data from outside should be done through public member functions The base class should allocate storage for the following data (and only this data) o student's first name (you may assume 20 characters or less) o student's last name (you may assume 20 characters or less) o Which course the student is in (English, History, or Math) 2. Each class should have a function that will compute and return the student's final average, based on the stored grades. All grades are based on a 100-point scale. Here are the grades that need storing for each subject, along with the breakdown for computing each final grade: English-Term Paper = 25%, Midterm = 35%, Final Exam-40% History-Attendance-10%, Project = 30%, Midterm = 30%, Final Exam = 30% Math -- There are 5 quizzes, to be averaged into one Quiz Average (which can be a decimal number). Final grade computed as follows: * Quiz Average = 15%, Test 1 = 25%, Test 2 = 25%, Final Exam = 35% 3. Write a main program (in a separate file) that does the following (in this order) a) Ask the user for input and output file names. This is the only input and output that should be done from keyboard and to the screen. All other input and output will be to and from files. (See the sample run below) b) Read the student data from the input file and store it using an array of appropriate type. You should use just one array for all students, not a separate array for each subject (i.e. this will be a heterogeneous list). You will need to allocate this list dynamically, since the size is stored in the input file. (Note that this also means each list item will need to be created dynamically). Each student's data should be stored in a separate object. (Any dynamically allocated space should be cleaned up appropriately with delete when you are finished with it)Explanation / Answer
main.cpp
using namespace std;
#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream> //library importations
#include <iomanip>
#include <cstdlib>
#include "grades.h"
int main()
{
ifstream fin; //variable declerations
ofstream fout;
char ifile[80];
char ofile[80];
int size;
int i1, i2, i3, i4, i5, i6, i7, i8;
do
{
fin.clear(); //clears input stream after each loop run
cout << "Please enter the name of the input file. ";
cout << "Filename: ";
cin >> ifile; //takes in input file name
fin.open(ifile);
if(!fin) //if it doesn't open, b/c wrong name, prompts for new name
{
cout << "This is not a valid file. Try again!" << endl;
cout << "Please enter the name of the input file." << endl << "Filename: ";
cin >> ifile;
fin.open(ifile);
}
}while(!fin); //runs until correct opens
cout << "Please enter the name of the output file. ";
cout << "Filename: ";
cin >> ofile; //takes in name of output file from standard input
fin >> size; //reads size from file
fin.ignore(); //ignores
Student ** list = new Student * [size]; //creates Student array of pointers of size
for(int i = 0 ; i < size; ++i) //runs through each pointer in array
{
char x[20], y[20], z[20];
fin.getline(x,20,','); //stores last name into x up to ','
fin.ignore(); //ignores following space
fin.getline(y, 20); //stores first name into last up to ' '
fin.getline(z, 20, ' '); //takes in course into z up until a space
if(strcmp(z, "Math") == 0) //if the course is math
{
fin >> i1 >> i2 >> i3 >> i4 >> i5 >> i6 >> i7 >> i8; //reads in math data
list[i]=new Math(i1,i2,i3,i4,i5,i6,i7,i8); //dynamically allocates Math obj of math data
list[i]->setfirst(y); //sets obj member data to read in math data
list[i]->setlast(x);
list[i]->settype(z);
}
if(strcmp(z, "English")==0) //if the course is english
{
fin >> i1 >> i2 >> i3; //reads in enligh data
list[i]=new English(i1,i2,i3); //same as above
list[i]->setfirst(y);
list[i]->setlast(x);
list[i]->settype(z);
}
if(strcmp(z, "History")==0) //same as above for history
{
fin >> i1 >> i2 >> i3 >> i4;
list[i]=new History(i1,i2,i3,i4);
list[i]->setfirst(y);
list[i]->setlast(x);
list[i]->settype(z);
}
fin.ignore(); //ignores new line character, every time loop runs
}
fin.close(); //closes input file
fout.clear(); //clears output stream
fout.open(ofile); //opens output file
// capture current output stream settings
int oldprecision = fout.precision(); //stores variables for standard flags
ios_base::fmtflags oldflags = fout.flags();
// do my output changes
fout.setf(ios::right);
fout << fixed; //sets appropriate flags, precision
fout << setprecision(2);
fout << "Student Grade Summary" << endl;
fout << "--------------------- ";
fout << "ENGLISH CLASS ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++) //runs through array
{
if(strcmp(list[i]->gettype(), "English")==0) //if the pointer points to an English obj
{
//displays first name, last name, final exam, average in class, and letter grade
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast())))) //sets spacing so its the same regardless of name length
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "HISTORY CLASS ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++)
{
if(strcmp(list[i]->gettype(), "History")==0) //same for history obj
{
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast()))))
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++)
{
if(strcmp(list[i]->gettype(), "Math")==0) //same for math obj
{
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast()))))
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "OVERALL GRADE DISTRIBUTION ";
int acount=0; //initializes count variables
int bcount=0;
int ccount=0;
int dcount=0;
int fcount=0;
for(int i=0;i<size;i++) //runs through array
{
if(list[i]->getlettergrade()=='A') //if the letter grade of pointed obj matches specific letter
acount++; //increase that counter
if(list[i]->getlettergrade()=='B')
bcount++;
if(list[i]->getlettergrade()=='C')
ccount++;
if(list[i]->getlettergrade()=='D')
dcount++;
if(list[i]->getlettergrade()=='F')
fcount++;
}
fout << "A: " << acount << endl; //displays distribution of counters
fout << "B: " << bcount << endl;
fout << "C: " << ccount << endl;
fout << "D: " << dcount << endl;
fout << "F: " << fcount << endl;
// PUT THINGS BACK THE WAY THEY WERE WHEN I FOUND THEM
fout.precision(oldprecision); // restore old precision setting
fout.flags(oldflags); // restore all prior format flags
fout.close(); //closes output file
for (int r=0; r < size; r++) //deletes dynamically allocated objs
{
delete list[r];
}
delete [] list; //deletes array of pointers (clears up space)
cout << "Processing complete ";
return 0;
}
grades.cpp
#include "grades.h"
#include <cctype>
#include <cstring>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
//takes in test scores as paramaters, sets member data
Math::Math(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
{
quiz1=i1;
quiz2=i2;
quiz3=i3;
quiz4=i4;
quiz5=i5;
test1=i6;
test2=i7;
finalexam=i8;
}
//same
History::History(int i1, int i2, int i3, int i4)
{
attendance=i1;
project=i2;
midterm=i3;
finalexam=i4;
}
//same
English::English(int i1, int i2, int i3)
{
paper=i1;
midterm=i2;
finalexam=i3;
}
//copies paramater into member data
void Student::setfirst(char* x)
{
strncpy(first, x, 20);
}
void Student::setlast(char* y)
{
strncpy(last, y, 20);
}
void Student::settype(char* z)
{
strncpy(course, z, 20);
}
//returns member data
const char* Student::getfirst() const
{
return first;
}
const char* Student::getlast() const
{
return last;
}
const char* Student::gettype() const
{
return course;
}
//computers average member data for each class
double English::GetAverage()
{
englishavg=(paper*.25)+(midterm*.35)+(finalexam*.4);
return englishavg;
}
double Math::GetAverage()
{
quizavg=(quiz1+quiz2+quiz3+quiz4+quiz5)/5.0;
mathavg= (quizavg*.15)+(test1*.25)+(test2*.25)+(finalexam*.35);
return mathavg;
}
double History::GetAverage()
{
historyavg=(attendance*.1)+(project*.3)+(midterm*.3)+(finalexam*.3);
return historyavg;
}
//returns respective finalexam scores for each class
int English::getfinalexam()
{
return finalexam;
}
int History::getfinalexam()
{
return finalexam;
}
int Math::getfinalexam()
{
return finalexam;
}
//computes letter grade for each specific class based off their letter grade
char English::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
char History::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
char Math::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
grades.h
#include <cstring>
#include <cctype>
#include <fstream> //library importations
#include <iomanip>
#include <cstdlib>
#include <string>
class Student
{
public:
//manipulators of private member data
void setfirst(char* x);
void setlast(char* y);
void settype(char* z);
//retrivers of privatre member data
const char* gettype() const;
const char* getfirst() const;
const char* getlast() const;
//abstract virtual class, functions for child classes
virtual int getfinalexam()=0;
virtual char getlettergrade()=0;
virtual double GetAverage() = 0;
private:
protected:
char first[20];
char last[20];
char course[20];
};
class Math: public Student
{
public:
Math(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int quiz1, quiz2, quiz3, quiz4, quiz5, test1, test2, finalexam;
double mathavg, quizavg;
protected:
};
class History: public Student
{
public:
History(int i1, int i2, int i3, int i4); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int attendance, project, midterm, finalexam;
double historyavg;
protected:
};
class English:public Student
{
public:
English(int i1, int i2, int i3); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int paper, midterm, finalexam;
double englishavg;
protected:
};
test.txt
6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
English 88 75 90
Dipwart, Marvin
History 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
History 40 100 68 88
Lewinsky, Monica
English 60 72 78
output.txt
Student Grade Summary
---------------------
ENGLISH CLASS
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Joe Schmuckatelli 90 84.25 B
Monica Lewinsky 78 71.40 C
HISTORY CLASS
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Marvin Dipwart 88 80.30 B
James T. Kirk 88 80.80 B
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Bugs Bunny 93 94.83 A
Jimmy Crack Corn 68 65.33 D
OVERALL GRADE DISTRIBUTION
A: 1
B: 3
C: 1
D: 1
F: 0