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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Student
{
private:
//Delcare variables
string name;
int id;
int *testptr;
int num;
//Member functions
void makeArray()
{
testptr = new int[num]; // Allocate num ints and save ptr in testptr.
for (int i=0; i<num; i++)
{
testptr[i] = 0; // Initialize all elements to zero.
}
}
public:
//Member Functions
Student()
{
setName("None");
setID(10);
num = 3;
makeArray();
}
Student(int n)
{
setName("None");
setID(10);
if (n > 0)
{
num = n;
}
else
{
num = 3;
}
makeArray();
}
Student(string nm, int i, int n)
{
setName(nm);
setID(i);
if (n > 0)
{
num = n;
}
else
{
num = 3;
}
makeArray();
}
void setName(string nm)
{
name = nm;
}
void setID(int i)
{
if (i >=10 && i <=99)
{
id = i;
}
else
{
id = 10;
cout << "Error. Can't set " << getName() << "'s id to i." << endl;
}
}
void setScore(int i, int s)
{
if(i < num) //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;
}
}
string getName() const
{
return name;
}
int getID() const
{
return id;
}
void showScore()
{
for (int i=0; i<num; i++)
{
cout << "Test " << i << " had a score of " << testptr[i] << endl; //Displays the test number and the score
}
}
void display()
{
cout << "The Name: " << getName();
cout << "The ID: " << getID();
showScore();
cout << endl;
cout << endl;
}
~Student()
{
delete[]testptr;
}
};
int main()
{
Student studentA;
Student studentB(4);
Student studentC("Joe", 40, 5);
studentA.setName("Tom");
studentA.setID(200);
studentA.setID(20);
studentA.setScore(0, 75);
studentA.setScore(1, 85);
studentA.setScore(2, 95);
studentB.setName("John");
studentB.setID(30);
studentB.setScore(0, 70);
studentB.setScore(1, 80);
studentB.setScore(2, 90);
studentB.setScore(3, 100);
studentC.setScore(0, 90);
studentC.setScore(1, 91);
studentC.setScore(2, 92);
studentC.setScore(3, 93);
studentC.setScore(4, 94);
studentC.setScore(5, 95);
studentC.setScore(4, 105);
studentC.setScore(5, 105);
studentA.display();
studentB.display();
studentC.display();
system("pause");
return 0;
}
|