1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class StudentTestScores;
const double NA= 0;
class StudentTestScores
{
private:
string studentName;
double *testScores;
int numTestScores;
char LetterGrade;
void createTestScoresArray(int size)
{
numTestScores=size;
testScores=new double[size];
for (int i=0; i<size; i++)
{
testScores[i]=NA;
}
}
public:
// class constructor
StudentTestScores();
StudentTestScores(string name, int numScores )
{
studentName=name;
createTestScoresArray(numScores);
}
// class copy constructor
StudentTestScores(const StudentTestScores &other)
{
studentName = other.studentName;
numTestScores = other.numTestScores;
testScores = new double[numTestScores];
for (int i = 0; i < numTestScores; i++)
{
testScores[i] = other.testScores[i];
}
}
// class destructor
~StudentTestScores()
{ delete [] testScores;}
// accessors & mutators
void setLetterGrade(char x)
{ LetterGrade=x;}
char getLetterGrade()
{ return LetterGrade;}
void setName(string x)
{ studentName=x;}
string getName() const
{ return studentName;}
void setTestScore(double score, int index)
{ testScores[index] = score;}
double getTestScore(int index)const
{ return testScores[index];}
void setNumTestScores(int x)
{ numTestScores=x;}
int getNumTestScores() const
{ return numTestScores;}
//Assignment operator overload
void operator=(const StudentTestScores &other)
{
delete [] testScores;
studentName = other.studentName;
numTestScores =other.numTestScores;
testScores = new double[numTestScores];
for(int i = 0; i < numTestScores; i++)
{
testScores[i] = other.testScores[i];
}
}
friend ostream& operator <<(ostream &, StudentTestScores &);
friend istream& operator >>(istream &, StudentTestScores &);
friend ifstream& operator >>(ifstream &, StudentTestScores &);
//displayInfo prototype
void displayInfo(StudentTestScores);
};
//Overload the Stream I/O operators.
ostream& operator <<(ostream &os, StudentTestScores &x)
{
os<< x.getName() << "\t" << x.getNumTestScores() << "\t" << x.getLetterGrade()<<"\t";
for (int i = 0; i < x.getNumTestScores(); i++)
{
os<< x.getTestScore(i)<<", ";
}
cout<<".\n";
return os;
}
istream& operator >>(istream &is, StudentTestScores &object)
{
cin.get();
cout<<"Enter student name : ";
getline(is,object.studentName);
cout<<"Enter number of test scores : ";
is>>object.numTestScores;
object.testScores=new double[object.numTestScores];
for(int i=0; i<object.numTestScores;i++)
{
cout<<"Enter score for test"<<(i+1)<<" : ";
is>>object.testScores[i];
}
return is;
}
ifstream& operator >>(ifstream &file, StudentTestScores &obj)
{
int numTestScores=0;
float sum=0;
string Name;
getline(file, Name, '\t');
file>>numTestScores;
StudentTestScores(Name, numTestScores);
for(int i=0; i<obj.numTestScores;i++)
{
file>>obj.testScores[i];
sum=sum+obj.testScores[i];
}
if (sum>=90 && sum<=100)
obj.setLetterGrade('A');
else if (sum>=80 && sum<=89)
obj.setLetterGrade('B');
else if (sum>=70 && sum<=79)
obj.setLetterGrade('C');
else if (sum<=69&& sum>=0)
obj.setLetterGrade('D');
else
obj.setLetterGrade('N');
return file;
}
#endif
|