#include #include #include using std::cout; using std::endl; using std::fixed; using std::setprecision; class Student { private: // Data members char name[31]; double gpa; public: // Constructors Student(); Student(const char*, double); // Accessor and mutator member functions void set_name(const char*); const char* get_name() const; void set_gpa(double); double get_gpa() const; // Other member functions void print() const; }; int main() { Student s1; // Initialized by default constructor. Student s2("Anna Gonzalez", 3.65); // Initialized by parameterized constructor. const Student s3("Todd Bell", 3.24); // Initialized by parameterized constructor. // Test object s1. s1.print(); cout << endl << s1.get_name() << endl; cout << s1.get_gpa() << endl << endl; s1.set_name("Shannon Willis"); s1.set_gpa(3.54); s1.print(); cout << endl << s1.get_name() << endl; cout << s1.get_gpa() << endl << endl; // Test object s2. s2.print(); cout << endl << s2.get_name() << endl; cout << s2.get_gpa() << endl << endl; s2.set_name("Anna Castro"); s2.set_gpa(3.72); s2.print(); cout << endl << s2.get_name() << endl; cout << s2.get_gpa() << endl << endl; // Test object s3. Object is constant, so calling a non-constant // mutator member function will cause a syntax error. s3.print(); cout << endl << s3.get_name() << endl; cout << s3.get_gpa() << endl; return 0; } // Student constructors Student::Student() { strcpy(name, "None"); gpa = 0.0; } Student::Student(const char* new_name, double new_gpa) { set_name(new_name); set_gpa(new_gpa); } // Student accessor and mutator member functions void Student::set_name(const char* new_name) { strcpy(name, new_name); } const char* Student::get_name() const { return name; } void Student::set_gpa(double new_gpa) { if (new_gpa < 0.0) gpa = 0.0; else if (new_gpa > 4.0) gpa = 4.0; else gpa = new_gpa; } double Student::get_gpa() const { return gpa; } // Other member functions void Student::print() const { cout << "Name: " << name << endl << "GPA: " << fixed << setprecision(2) << gpa << endl; }