[Solved] Objective Lab Give Students Practice Object Oriented Programming Particular Process Buildi Q37185214
The objective of this lab is to give students more practice withobject oriented programming. In particular, the process of buildingclasses on top of other classes. We will start by compiling andtesting the “Student” class from the previous lab. Then we will usethe Student class to create a “Course” class that can store basicinformation about multiple students.
Instructions:
Consider the following C++ program. At the top you can see theinterface for the Student class. Below this is the implementationof the Student class methods. Finally, we have a very small mainprogram.
#include <string>#include <fstream>#include <iostream>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 classn”; Student student1; student1.Set(1234, “John”, 2.5); student1.Print(); return 0;}
Step 1: Copy this program into your C++ programeditor, and compile it. Hopefully you will not get any errormessages. When you run your program it should print the followingfamiliar messages:
Testing Student class 1234 John 2.5
Step 2: Our goal is to create a Course classusing the Student class. To do this, copy and paste the followingclass definition just before the main function in your code:
class Course { public: Course(const int count=0); Course(constCourse & course); ~Course(); void Print() const; void Read();private: static const int MAX_STUDENTS = 100; Studentstudents[MAX_STUDENTS]; int num_students; };
Notice that the Course class contains an array of 100 studentobjects. This will let us store information for up to 100 students.The variable “num_students” is used to keep track of how many ofthe 100 array locations we are actually using at any giventime.
Step 3: If you compile and test your newprogram, you will see that it will output the same messages asbefore because it is not using the “Course” class in any way. Edityour main function and add the following lines to the bottom of thefunction:
cout << “Testing Course classn”; Course course(5);course.Print();
Step 4: When you compile the program this time,you should get error messages saying that several Course methodsare undefined. To make the messages go away, create “skeletonmethods” in the Course class by copying the method headers belowthe Course class definition to create “empty” methods that justprint out the names of the methods. For the constructor destructormethods print out “Constructor”, “Copy constructor” and”Destructor”. When you recompile and run your program it shouldprint out the following:
Testing Student class 1234 John 2.5 Testing Course classConstructor Print Destructor
Step 5: Now it is time to complete theimplementation of the Course class. Edit your program to fill inthe missing code in these methods. The default constructor istrivial because it is just one line long.
num_students = count;
The copy constructor, the Print method, and the Read method mustprocess an array of student records. All of these methods require aloop of the form:
for (int index = 0; index < num_students; index++) { // Dosome 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 acall to one of the methods in the Student class.
Step 6: Once you have completed theimplementation 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 classConstructor Print 0 none 0 0 none 0 0 none 0 0 none 0 0 none 0Destructor
Step 7: There is one method in the Course classthat we have not tested yet, and that is the “Read” method. Beforewe do this, create a text file called “student.txt” and copy andpaste the following information into this file.
1234 Susan 3.9 2345 John 3.2 3456 Laura 3.8 4567 Brian 3.5 5678David 3.1
Now, edit your main function, and add the following lines to thebottom of the function:
course.Read(); course.Print();
Step 8: When you compile and run your programthis time, you should see the same messages as before, and yourprogram will pause after printing “Read” on the screen. As you canguess, the program is waiting for you to type in five studentrecords. To save yourself time, you can cut and paste the fivelines from “students.txt” into your program. When you do this, youshould see the same information printed back out again.
Another way to test your program is to use the Linux fileredirection feature. If your program is called “lab.exe” you cantype “./lab.exe < student.txt” to run the program and thecontents of student.txt will be read into the program. If you dothis, you should see the following printed:
Testing Student class 1234 John 2.5 Testing Course classConstructor Print 0 none 0 0 none 0 0 none 0 0 none 0 0 none 0 ReadPrint 1234 Susan 3.9 2345 John 3.2 3456 Laura 3.8 4567 Brian 3.55678 David 3.1 Destructor
Step 9: Once you think your program is workingcorrectly, save it somewhere safe, because youwill need it for lab12c. Finally, upload your final program intothe auto grader by following the instructions below.
Expert Answer
Answer to The objective of this lab is to give students more practice with object oriented programming. In particular, the process… . . .
OR

