Question

Instructions: Consider the following C++ program. At the top you can see the interface for the...

Instructions:

Consider the following C++ program. At the top you can see the interface for the Student class. Below this is the implementation of the Student class methods. Finally, we have a very small main program.

#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 class\n";
   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\n"; 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

main.cpp

#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;
}

class Course {
public: Course(const int count=0){
   num_students = count;
   cout<<"Constructor"<<endl;
}
Course(const Course & course){
   cout<<"Copy constructor"<<endl;
   num_students = course.num_students;
   for (int index = 0; index < course.num_students; index++) {
       students[index] = course.students[index];
   }
}

~Course(){
   cout<<"Destructor"<<endl;
}
void Print() const;
void Read();
private:
static const int MAX_STUDENTS = 100;
Student students[MAX_STUDENTS];
int num_students;
};

void Course::Print()const{
   cout<<"Print"<<endl;
   for (int index = 0; index < num_students; index++) {
       students[index].Print();
   }
}

void Course::Read(){
   cout<<"Read"<<endl;
   for (int index = 0; index < num_students; index++) {
       students[index].Read();
   }
}

int main()
{
   cout << "Testing Student class\n";
   Student student1;
   student1.Set(1234, "John", 2.5);
   student1.Print();
   cout << "Testing Course class\n";
   Course course(5);
   course.Print();
   course.Read(); course.Print();
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
Instructions: Consider the following C++ program. At the top you can see the interface for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT