CSCE 2004 - Laboratory Assignment 12

The objective of this laboratory assignment is to build upon the experience you have gained in the previous lab in using classes and objects. In particular, this assignment primarily emphasizes the techniques required to fill complex data structures (in the context of nested classes and arrays of objects) by reading data from a file with a single method call. It has the following steps:

Consider the two class definitions given below:


student.h:



#include <fstream>

#include <string>

using namespace std;



class Student

{

public: 

    Student();

    bool readData(ifstream & din);

    void setInfo (const int UAID, const string Name, const float GPA);

    void print();

private: 

    int uaid;

    string name;

    float gpa;

};





course.h:



#include <fstream>

#include "student.h"

using namespace std;



const int MAX_STUDENTS=200;



class Course

{

public: 

    Course();

    bool fillArray(const string filename);

    void print();

private: 

    Student students[MAX_STUDENTS];

    int numStudents;

};





student.dat:



1234 Susan 3.2

5678 Brian 3.5

9012 Laura 3.8

2345 David 3.1



As you can see, the classes are used to keep track of the grades in one course for students in a class. The base class Student is used to keep hold of the information for a student. It can be used to hold the UAID, the name, and the GPA of a student. The method Student::readData() reads the three pieces of information for a student from an input stream (that is used to open a file). It returns a Boolean value that is set to false if the file read fails (if everything else is fine, this happens only when the end-of-file is encountered). The method Student::print() prints the information of a student to the screen.

The Course class holds an array of Student objects, and hence is able to hold the information of all the students in the class. It is through the methods of this class that you actually can hold and retrieve information for the students in the class. The method Course::print() makes calls to Student::print() to print out the information for all the students in the class. The function Course::fillArray() is the method which makes calls to the Student::readData() method in the base class and stores the information for all the students in the class at once from the input stream provided.

You are provided with the methods Student::readData() and Student::setInfo(). They are defined as below:


student.cpp:



#include <iostream>

#include <fstream>

#include <string>

#include "student.h"

using namespace std;



Student::Student()

{

    uaid = -1;

    name = "";

    gpa = -1.0;

}

 

bool Student::readData(ifstream & din)

{

    bool success;

    

    if(din >> uaid >> name >> gpa)

        success = true; 

    else

        success = false;

 

    return success;

}

 

void Student::setInfo(const int UAID, const string Name, const float GPA)

{

    uaid = UAID;

    name = Name;

    gpa = GPA;

}

 

void Student::print()

{

   //fill me with code!

}



 

course.cpp:



#include <iostream>

#include <fstream>

#include <string>

#include "course.h"

using namespace std;

 

Course::Course()

{

    numStudents = 0;

}

 

bool Course::fillArray(const string filename)

{

    return true; //temporary placeholder to allow everything to compile

}



void Course::print()

{

    //fill me with code!

}



 

lab12.cpp (alternatively, main.cpp):



#include <iostream>

#include <fstream>

#include <string>

#include "course.h"

using namespace std;

 

int main()

{

    Student aStudent;

    Course CSCE2004;

    ifstream din;

 

    return 0;

}       



  1. Getting Started
  2. Begin by creating the files (student.dat, course.h, course.cpp, student.h, student.cpp, lab12.cpp), copy/pasting the code into the appropriate files, compiling, and running the project.

  3. Building up, bit by bit
  4. Now, beginning with the lowest-level unfinished method and proceeding until every method is complete, you will add code for each empty method and test it.

    For the first step in the process, fill in the body of the empty method in the Student class, Student::print(). Then in the main program, open the file, student.dat, and loop until the end of the file appropriately calling Student::readData() and Student::print() in order to echo the data file to the screen.

    Note that this first step is just to make sure we can access the file using an object. Unfortunately, every time we read in data for a new Student object, we destroy the old data by writing over it. In the next step, we will work to fix this.

  5. Let's go to Courses!
  6. In this step, we will transfer the work begin done in main() from the previous step to a method of the Course class. We won't quite fix the problem of destroying data as we read it, but we'll get closer!

    In Course::fillArray(), declare a local Student object. For example,

    
    Student aStudent;
    
    
    Take the code introduced in main() in the previous step, and move it to Course::fillArray(). That is, inside the fillArray() method, a local Student object will read subsequent student records from the file and display them to the screen until the end of the file.

  7. File I/O with nested objects
  8. Finally, we will substitute the array of Student objects in the class definition of Course for the local Student object used in the previous step. This way, each subsequent Student object in the array can read in a subsequent student record from the file. Since we have an array of Student objects, we should no longer lose any data, since each Student object will read in data only once.

    Begin by removing the declaration of the local Student object, aStudent, from the Course::fillArray() method. Then in the places you had used aStudent previously, substitute a member of the array of Student objects contained in the Course class definition.

    In particular, for the i'th read from the file, you will want to use the i'th Student object, i.e. students[i]. Note that you should do the same for the calls to Student::print(), so that the method prints out the data to the screen as it's storing the data in the array of Student objects.

    Finally, make sure that numStudents is set appropriately by fillArray().

  9. Wrapping up
  10. Finally, we will separate the functionalities of reading from the file and printing to the screen.

    Fill in the Course::print() method. In particular, it should loop over the length of the array of Student objects, calling Student::print() for each.

    Then, remove all of the print statements from fillArray().

    Finally, in the main program, ensure that you make calls to both Course::fillArray() and Course::print() using a Course object. You should see the data for all of the students in the file on the screen.

  11. Homework 7 and final news
  12. Take some time to check out the assignment description of Homework #7. This is the final homework assignment for the class and is not to be taken lightly! Note that you can work on HW #7 in groups of two, so long as your partner is someone in your lab section.

    If you haven't yet found a partner but would like one, see if you can find one now. Alternatively, you can talk to the TA about helping you find a partner. Also, see if you have any questions over Homework 7 for the TA. Now is a great time to get anything answered that you need!

    Finally: This is the last official meeting of the lab sections. However, there will be a lab assignment next week - the Practice Final Exam. It will be posted at the start of the week, and you will email your solutions to your TA before the final exam time to receive a lab assignment credit.

    There will be an optional, class-wide Final Exam review session in JBHT 144 on Tuesday, May 3rd from 3:30pm-5:30pm. The format of the review will be question-and-answer, so come armed with questions you'd like discussed!

  13. Submit Your Work
  14. To submit your lab work, have your TA check over any code/output that you've done during this lab session and record that you completed the lab assignment.

    Important Note: It is your responsibility, not the TA's, to ensure that your lab is recorded as being completed before you leave! Although the labs are intended to be completed in the alloted lab time, if you need additional time to finish the lab assignment, you have 24 hours from the end of the lab meeting time to finish. If so, you should ask your TA for instructions on how to submit the lab assignment after the lab ends.

  15. Logout of the System
  16. After making sure that you submitted the code correctly and you have done the entire assignment, logout of the system by clicking on the menu button in the lower left hand side of the desktop. Then, click on Log Off. Wait a few seconds for a window to pop up and then click Log Off.