CS 12 - Lab 9

In this lab you are going to use class composition and a friend function. You will need the SSN class you developed in lab6. You will also use a makefile to compile your program.

Note that the program must be written, compiled, and executed under Linux.

Create a directory called lab9 and copy the ssn.cpp and ssn.h files from your lab6 directory into it using the cp command.

The Program

Write a class Student that maintains information about a student. The class should have the following data members: The class should contain a constructor that takes no arguments and simply initializes the member data variables to "empty" values; i.e. the first and last names should be set to an empty string (i.e. ""), the GPA to 0.0, and the SSN to all 0's (note that the SSN's constructor does the latter).

The class should also define the following methods:

Details

You need to create a C++ header file, student.h, that contains the definition for your student class. Make sure you include ssn.h at the top of this file. You also need to create a C++ file, student.cpp, in which to place the implementation for the class. You need to include student.h at the top of this file.

Following are descriptions of the needed methods:

bool setData( char *fname, char *lname, double GPA, char *ssn )
This class method takes a first name, a last name, a GPA, and SSN to set the data to.

Note that to set the SSN, the setSSN() method from the SSN class should be called. Remember, this method will return false if the SSN is not valid.

If the GPA is less than 0.0 or the SSN is not valid, the method should do nothing but return false, else it should set the data members and return true. You may assume that the first and last names will always be valid.

In order to save the first and last names passed in, use the strdup function, which duplicates (i.e. makes a copy of) a string. To use this function, you will need to include string.h. The prototype for strdup is:

   char *strdup( const char *s );
It takes a string as a parameter and returns a duplicated string. Following is a code example to show how the function is used. It will print out Hello World.

   char *copy;
   copy = strdup( "Hello World" );
   cout << copy << endl;
It may not be obvious why we need to make a duplicate of the names when saving them. The reason will be explained in class.
void print()
This class method prints all of the information about a student in the following format:

   Name: Smith, Joe
   SSN:  555-55-5555
   GPA:  3.75
Note that the print() method in the SSN class should be called to print the SSN. Also, the GPA should be rounded to 2 decimal places.
void bestGPA( const Student &s1, const Student &s2 )
This method is not a class method, but a friend function. It compares two students' GPAs and prints a message stating which has the better GPA, as well as both GPAs. For example, a message such as:

   Jane Doe has a GPA of 3.50, which is better than John Doe, who has a GPA of 3.04.
would be printed if Jane had a better GPA than John.

Main Program

Place your main program in the file main.cpp. The function should create two Student objects, then fill the objects with information supplied by the user. In other words, the program should prompt the user for each student's first and last name, GPA, and SSN. If the user supplies bad data for the GPA (i.e. it's negative) or SSN, the program should prompt for the information again, until the user enters valid information. Remember, setData will return false if it receives invalid information, so you only need to check its return value to see if you need to re-prompt the user for information.

Once the information on the two students has been correctly entered, the program should print out the information about both students using the print method, then print out which has the better GPA by calling the bestGPA friend function.

Compiling With a Makefile

To compile your program, you could use the g++ command, but now that we have broken up the program into 5 files (main.cpp, student.cpp, student.h, ssn.cpp, and ssn.h), using g++ directly is getting inconvenient. Fortunately, there is an alternative: a makefile.

A makefile is simply a textfile that contains commands on how to compile your program. I have provided a makefile for this lab; click here to see it. Once you have saved the makefile in your lab9 directory, you can compile your program by simply typing in the command make.

When you type make, the compiler will automatically compile each of your source (i.e. .cpp) files. Assuming there are no errors, it will produce an executable file called lab9. To execute your program, then, you simply type in the command lab9.

For more information on makefiles, click here.

Grading

When you have finished the program, your TA will grade it. To receive full credit, your TA will check all of the following: