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
|
#include <string>
#include <iostream>
#include <iomanip>
class Major
{
public:
bool good() const { return true;} // returns true if the class is valid
std::string name;
};
class Student
{
private: // Members
int m_id;
std::string m_name;
Major m_major;
int m_nbCredits;
float m_gpa;
std::string m_level;
public:
static float s_avgGpa;
static float s_totalGpa;
static int s_nbStudents;
public: // Contrustors
Student() : m_id(0), m_name(""), m_nbCredits(0), m_gpa(0.0f), m_level("")
{
++s_nbStudents;
}
Student(int id, std::string name, Major& major, int credits, float gpa)
{
++s_nbStudents;
set(id, name, major, credits, gpa);
}
~Student()
{
s_totalGpa -= m_gpa;
--s_nbStudents;
std::cout << m_name << " has been removed." << std::endl;
}
public: // Set Functions
bool setID(const int id) // returns true if successful
{
if (id < 1) // ID must be > 0
return false;
m_id = id;
return true;
}
bool setName(const std::string& name) // returns true if successful
{
if (name.empty()) // Input most not be empty
return false;
m_name = name;
return true;
}
bool setMajor(const Major& major)
{
if (!major.good()) // Some arbirary check for whether major is valid
return false;
m_major = major;
setLevel(); // This really doesn't do anything
return true;
}
bool setCredits(const int credits)
{
if (credits<0) // credits must be positive
return false;
m_nbCredits = credits;
setLevel();
return true;
}
bool setGpa(const float gpa)
{
if (gpa < 0.0 || gpa > 5.0) // GPA must be within this range
return false;
// handling the total GPA
s_totalGpa -= m_gpa;
s_totalGpa += gpa;
// Setting the value
m_gpa = gpa;
// Calculating the average GPA
if (s_nbStudents > 0)
s_avgGpa = s_totalGpa / s_nbStudents;
else
s_avgGpa = 0.0f;
return true;
}
bool set(const int id, const std::string& name, const Major& major, const int credits, const float gpa)
{
bool result = setID(id);
result = setName(name) && result;
result = setMajor(major) && result;
result = setCredits(credits) && result;
result = setGpa(gpa) && result;
return result;
}
public: // Get functions
const int getID() { return m_id; }
const std::string getName() { return m_name; }
const Major getMajor() { return m_major; }
const int getCredits() { return m_nbCredits; }
const float getGpa() { return m_gpa; }
const std::string getLevel() { return m_level; }
static float getAvgGpa() { return s_avgGpa; }
public: // I/O
void print()
{
std::cout << getID() << '\t';
std::cout << getName() << '\t';
std::cout << getMajor().name << '\t';
std::cout << getCredits() << '\t';
std::cout << std::setprecision(2) << getGpa() << std::endl;
}
private: // Utilities
void setLevel()
{
if (m_nbCredits < 31) m_level = "Sophomore";
else if (m_nbCredits < 61) m_level = "Junior";
else m_level = "Senior";
}
};
int Student::s_nbStudents = 0;
float Student::s_totalGpa = 0.0f;
float Student::s_avgGpa = 0.0f;
int main()
{
Student s;
Major m;
s.set(5, "Henry", m, 35, 3.6f );
s.print();
return 0;
}
|