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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//student Class declaration
class Student
{
private:
string name;
int id;
int *test;
int num;
void makeArray();
public:
Student();
Student(int n);
Student(string nm, int i, int n);
void setName(string nm);
void setID(int i);
void setScore(int i, int s);
string getName() const;
int getID() const;
void showScore();
void display();
~Student();
};
//makeArray allocate an int array with num elements,
//assigns the address of the array to test and assigns 0 to all elements
void Student::makeArray()
{
int size = Student::num;
int *studentArray;
studentArray = new int[num];
Student::test = studentArray;
test = 0;
}
//Student function
Student::Student()
{
setName("NONE");
setID(10);
Student::num = 3;
}
//Second student function, takes in 2 parameters
Student::Student(int n)
{
setName("None");
setID(10);
if(n > 0){
Student::num = n;
}else{
Student::num = 3;
}
makeArray();
}
//Third student function, takes in 3 parameters
Student::Student(string nm, int i, int n)
{
setName(nm);
setID(i);
if(n > 0){
Student::num = n;
}else{
Student::num = 3;
}
makeArray();
}
//sets name to nm
void Student::setName(string nm)
{
Student::name = nm;
}
//sets id to i. If i is in range of 10 - 99. If not then it sets id to 10 and prits and error
//message saying it cannot set the id to i. The error message should include the students name
//by displaying the return value of get name
void Student::setID(int i)
{
if(i >= 10 && i <= 99){
Student::id = i;
}else{
Student::id = 10;
cout << "Invalid. Can not set id to " << i << " for " << getName() << endl;
}
}
//only sets the score if index i is a valid index within the bounds of the dynamic array holding the test scores,
//and if s is a valid score in teh range of 0-100. If it doesn't meet thse conditions, an error message should display
// saying the test i cantnot be set to s. Error message should include student's name by displaying return value or getName.
void Student::setScore(int i, int s)
{
if(i < Student::num){
if(s >= 0 && s <= 100){
test[i] = s;
}else{
cout << "Invalid. Can not set test " << i << " to " << s << " for " << getName() << endl;
}
}else{
cout << "Invalid. Can not set test " << i << " to " << s << " for " << getName() << endl;
}
}
//returns the name
string Student::getName() const
{
return Student::name;
}
//returns the id
int Student::getID() const
{
return Student::id;
}
//displays the test number and the score
void Student::showScore()
{
for(int count = 0; count < Student::num; count++)
{
cout << "Test " << count << "had a score of " << test[count] << endl;
}
}
//get and display the student's information
void Student::display()
{
cout << "The Name: " << getName();
cout << "The ID: " << getID();
showScore();
cout << endl;
cout << endl;
}
//frees the array that test is pointing to
Student::~Student()
{
free(test);
}
int main()
{
//sets up the 3 students
Student a;
Student b(4);
Student c("Joe", 40, 5);
//calls the set functions
cout << "Calling the set functions";
//for student a
a.setName("Tom");
a.setID(200);
a.setID(20);
a.setScore(0, 75);
a.setScore(1, 85);
a.setScore(2, 95);
//for student b
b.setName("John");
b.setID(30);
b.setScore(0, 70);
b.setScore(1, 80);
b.setScore(2, 90);
b.setScore(3, 100);
//for student c
c.setScore(0, 90);
c.setScore(1, 91);
c.setScore(2, 92);
c.setScore(3, 93);
c.setScore(4, 94);
c.setScore(5, 95);
c.setScore(4, 105);
c.setScore(5, 105);
//the display function
a.display();
b.display();
c.display();
system("pause");
return 0;
}
|