CS32, Fall 2012

Lab03:
Write, implement and apply a class


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 0: Choose initial pair programming roles, and log on

We hope you know the drill by now, so partner up (and remember to switch roles after awhile). If your regular partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.

Step 1: Create a directory for this lab, and get copies of lab files

This lab's pilot should log in. You will (both) work in this account for the rest of the lab.

Create a ~/cs32/lab03 directory and make it your current directory:

mkdir ~/cs32/lab03
cd ~/cs32/lab03

Then use cp to get copies of both grading1.cpp and grading2.cpp from the class account as follows:

cp ~cs32/labs/lab03/*.cpp ~/cs32/lab03

Linux note: If you are not copying/pasting, then please notice you are copying from ~cs32 to ~/cs32 and be sure you know the difference! Also remember that you can substitute a dot '.' for your current working directory.

Step 2: Distinguish a procedural from an object-oriented design

Look over grading1.cpp, the less object-oriented version of today's program. It does use a struct to manage a student's collection of grades, but that's as object-oriented as it gets. Essentially, this is a C program in C++ clothing - the overall program design is procedural. Still you should make sure you know what it does and how it works before improving it.

This program solves the textbook's Programming Project 1 on page 594. Briefly, it is a grading program for a class with these policies: (a) two quizzes worth 10 points each; (b) one midterm and one final exam worth 100 points each; (c) overall grade based on 50 percent for the final exam, 25 percent for midterm, and 25 percent for quizzes. Further, 90+ earns A, 80-90 earns B, 70-80 earns C, 60-70 earns D, and otherwise F.

The program reads 4 numbers from stdin, and calculates and prints the associated letter grade. It does no error-checking. Compile and execute it like so (user input is bold):

bash-4.2$ make grading1
g++     grading1.cpp   -o grading1
bash-4.2$ ./grading1
Enter scores in this order: 2 quizzes, midterm, final: 9 9 40 90
Grade is B

Notice an important feature of the code in grading1.cpp: the data values in the global structure definition are directly accessable to all parts of the program. Unless otherwise specified, all members of a C++ struct are public. These data values are manipulated in three of the external functions: getScores, overallGrade, and main. It would be difficult to reuse parts of this code in other programs, and also difficult for multiple programmers to work together on separate parts of the code.

Be sure you understand why this design is not optimal, before continuing to Step 3.

Step 3: Convert a struct to a class

Now it's time to do (a variation of) the textbook's Programming Project 2 on page 595, which is to "Redo Programming Project 1 ... but this time make the student record type a class type instead of a structure type."

Open grading2.cpp with an editor (e.g., emacs). No need to read the code, as it is unchanged from grading1.cpp - only the comments are new. We'll guide you through this first step of the conversion, to properly define class Record. Make just the following changes to the file.

  1. First type your name(s) in the comment at the top of the program. And if you want to be neat, then delete the (uppercase) instructions too - same goes for all parts, but just for neatness and not a requirement.
  2. Redefine a Record object to be a class instead of a structure, by changing line 7 as instructed.
  3. The first section of a class definition traditionally is its public interface. In C++ that means adding the keyword public and a colon (':') on a line by itself - otherwise, all parts of a class by default are private. Add that line now.
  4. In a moment you will make the data private, but that means you must provide a way for clients of the class to set data values, but only because this problem requires that clients have that capability. To that end, declare a method named setGrades in the public section. This method should take four double arguments, and its return type should be void. Just declare the method here - it will be implemented later. The declaration does not require names for the parameters, only types, but if you want to name them here anyway, we suggest q1, q2, m and f, in that order.
  5. Before the data values are declared, add the keyword private and a colon to identify the rest of the class as private parts.
  6. It's time for some cut and paste, and then some editing. Move the function prototype of letterEquiv into the private section of the class definition - it will become a class member function ("method"), but only to be used by other class members. Also add the keyword const to the end of its signature, so it can support constant objects.
  7. Now move the function prototype of overallGrade into the class's public section, so it becomes part of a Record's interface. This method will calculate a Record object's own letter grade, so it won't need any parameter! Therefore, remove the parameter from the signature, and add the keyword const to the end.

  8. By the way, be sure to leave the function prototype of getScores in-between the class definition and main - it will remain as an external function.

The program will not successfully compile now, but the part inside the class definition should compile without errors. Try compiling it now to verify that the first error is not until the second statement in main:

bash-4.2$ make grading2
g++     grading2.cpp   -o grading2
grading2.cpp: In function 'int main()':
...

'...' above means about 20 more lines of error messages. If you have errors before that point, then you should fix them before continuing to Step 4.

By the way, in case you need more guidance with this step or the next one, here is an example program that defines, applies and implements a simple date class, and that is very similar to the grading2.cpp program.

Step 4: Finish implementing the object-oriented design

Have you switched partner roles yet?

The (uppercase) comments in the rest of grading2.cpp include specific instructions as well as hints about what needs to be done to complete the conversion. Here are some general guidelines to help you understand why these changes must be made.

Compile and test the program to be sure it works correctly after all your changes. Fix any problems before proceeding to Step 5.

Step 5: Show off your work and get credit for the in-lab requirements

Get your TA's attention to inspect your work, and to record completion of your in-lab work.

Don't leave early though ... begin the after-lab work below.

Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

If you must complete this assignment at CSIL, then submit it with the turnin program - but do NOT turn it in if the TA already checked you off. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Cooper Lab.

Bring up a terminal window on CSIL, and cd into the original pilot's cs32 directory, and cd again into the lab03 directory. Then type the following to turn in your completed grading2.cpp:

turnin lab03@cs32 grading2.cpp

Evaluation and Grading

Each pair of students must accomplish the following to earn full credit for this lab:


After lab-work is done

Optional - or to do in lab after completing the required work

Make a copy of grading2.cpp named grading3.cpp, and work on this copy for these optional tasks. Do the tasks in any order, but be sure to fully test your changes after doing each one, before starting the next task.

Homework - to be done before lab next week: October 25

This week's homework includes adding features to class Record and testing them, as described in these hw3 instructions. You will use the turnin program to submit some of the work, and that must be done before your next lab - the TA will check for it then. A written part is also required, to be turned in at your lab next week. Please do NOT print the homework in Cooper Lab.

Each student must complete the paper part of the homework, even if you do the programming parts in pairs.


Prepared by Michael Costanzo.