Question: #include #include #include using namespace std; class Student { public: Student(); Student(const Student & student); ~Student(); void Set(const int uaid, const string name, const float
#include#include #include using namespace std; class Student { public: Student(); Student(const Student & student); ~Student(); void Set(const int uaid, const string name, const float gpa); void Get(int & uaid, string & name, float & gpa) const; void Print() const; void Read(); private: int Uaid; string Name; float Gpa; }; Student::Student() { Uaid = 0; Name = "none"; Gpa = 0; } Student::Student(const Student & student) { Uaid = student.Uaid; Name = student.Name; Gpa = student.Gpa; } Student::~Student() { } void Student::Set(const int uaid, const string name, const float gpa) { Uaid = uaid; Name = name; Gpa = gpa; } void Student::Get(int &uaid, string & name, float &gpa) const { uaid = Uaid; name = Name; gpa = Gpa; } void Student::Print() const { cout << Uaid << " " << Name << " " << Gpa << endl; } void Student::Read() { cin >> Uaid >> Name >> Gpa; } int main() { cout << "Testing Student class "; Student student1; student1.Set(1234, "John", 2.5); student1.Print(); return 0; }
Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages. When you run your program it should print the following:
Testing Student class 1234 John 2.5
Step 2: Edit your program and add cout statements to all of the Student class methods to print out the name of the method on one line. For the constructor destructor methods print out "Constructor", "Copy constructor" and "Destructor". When you recompile and run your program it should print out the following:
Testing Student class Constructor Set Print 1234 John 2.5 Destructor
Step 3: We have not tested the copy constructor yet. Edit your main program create a second student object using the copy constructor and call the Print method to print out the contents of this object.
Student student2(student1); student2.Print();
When you compile and run your program you should now see the following:
Testing Student class Constructor Set Print 1234 John 2.5 Copy constructor Print 1234 John 2.5 Destructor Destructor
Step 4: Edit your program and use the Set method to modify student2 so it contains "2345 Susan 3.9". Next, add another call to the Print method to print out this object. When you compile and run your program you should now see the following:
Testing Student class Constructor Set Print 1234 John 2.5 Copy constructor Print 1234 John 2.5 Set Print 2345 Susan 3.9 Destructor Destructor
Step 5: There is one key method we have not tested and that is the "Read" method. As you can see from the code, this method reads the three student fields using cin. At the very bottom of your main program, create a third student object and call the Read method and the Print method on this object. After you recompile your program, run it and type in "3456 Brian 3.5". Did the program print out what you expected? What happens if you type in "Brian 3456 3.5" instead?
Step 6: There is one major issue with the Student class; users can store GPA values that do not make any sense. To correct this, modify your program to perform range checking on GPA values. If the user attempts to store a GPA < 0.0, you should store 0.0. If they attempt to store a GPA > 4.0, you should store 4.0. You should add this error checking to all methods that can change the GPA value.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
