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; if (Gpa < 0.0) Gpa = 0.0; else if (Gpa > 4.0) Gpa = 4.0; } 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; if (Gpa < 0.0) Gpa = 0.0; else if (Gpa > 4.0) Gpa = 4.0; } 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 familiar messages:

 

Testing Student class 1234 John 2.5

Step 2: Our goal is to create a Course class using the Student class. To do this, copy and paste the following class definition just before the main function in your code:

 

class Course { public: Course(const int count=0); Course(const Course & course); ~Course(); void Print() const; void Read(); private: static const int MAX_STUDENTS = 100; Student students[MAX_STUDENTS]; int num_students; };

Notice that the Course class contains an array of 100 student objects. This will let us store information for up to 100 students. The variable "num_students" is used to keep track of how many of the 100 array locations we are actually using at any given time.

Step 3: If you compile and test your new program, you will see that it will output the same messages as before because it is not using the "Course" class in any way. Edit your main function and add the following lines to the bottom of the function:

 

cout << "Testing Course class "; Course course(5); course.Print();

Step 4: When you compile the program this time, you should get error messages saying that several Course methods are undefined. To make the messages go away, create "skeleton methods" in the Course class by copying the method headers below the Course class definition to create "empty" methods that just print out the names of the methods. 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 1234 John 2.5 Testing Course class Constructor Print Destructor

Step 5: Now it is time to complete the implementation of the Course class. Edit your program to fill in the missing code in these methods. The default constructor is trivial because it is just one line long.

 

num_students = count;

The copy constructor, the Print method, and the Read method must process an array of student records. All of these methods require a loop of the form:

 

for (int index = 0; index < num_students; index++) { // Do some work }

In order to perform operations on each of the student records, you need to make method calls of the form: "student[index].Method(...)" where you replace Method(...) with a call to one of the methods in the Student class.

Step 6: Once you have completed the implementation of the Course methods, compile and run your program. You should now see the following output:

 

Testing Student class 1234 John 2.5 Testing Course class Constructor Print 0 none 0 0 none 0 0 none 0 0 none 0 0 none 0 Destructor

Step 7: There is one method in the Course class that we have not tested yet, and that is the "Read" method. Before we do this, create a text file called "student.txt" and copy and paste the following information into this file.

 

1234 Susan 3.9 2345 John 3.2 3456 Laura 3.8 4567 Brian 3.5 5678 David 3.1

Now, edit your main function, and add the following lines to the bottom of the function:

 

course.Read(); course.Print();

Step 8: When you compile and run your program this time, you should see the same messages as before, and your program will pause after printing "Read" on the screen. As you can guess, the program is waiting for you to type in five student records. To save yourself time, you can cut and paste the five lines from "students.txt" into your program. When you do this, you should see the same information printed back out again.

Another way to test your program is to use the Linux file redirection feature. If your program is called "lab.exe" you can type "./lab.exe < student.txt" to run the program and the contents of student.txt will be read into the program. If you do this, you should see the following printed:

 

Testing Student class 1234 John 2.5 Testing Course class Constructor Print 0 none 0 0 none 0 0 none 0 0 none 0 0 none 0 Read Print 1234 Susan 3.9 2345 John 3.2 3456 Laura 3.8 4567 Brian 3.5 5678 David 3.1 Destructor

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!