By the time you have completed this lab, you should be able to
class
, and know how it differs from
a struct
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.
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.
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.
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.
class
instead of a
struct
ure, by changing line 7 as instructed.public
and
a colon (':
') on a line by itself - otherwise, all parts of a class by default
are private. Add that line now.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.private
and
a colon to identify the rest of the class as private parts.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.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.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.
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.
main
, the function overallGrade
is not being used
properly anymore, because overallGrade
is now a member function of the Record class, and it
no longer accepts an argument. Actually, now the object named rec
is able to calculate and return its own grade. Use the object's name and the
dot ('.
') operator to invoke the method (without any arguments).getScores
, currently the data values of the Record object
(result
) are being set directly. That can't work anymore. The only way
to set the values of the object's data is to use the public method
setGrades
.letterEquiv
and
overallGrade
both must be changed in two ways: (a) they must identify
themselves as part of class Record - using the scope resolution operator
('::
') correctly; and (b) they must be declared const
to match their new signatures in the class definition.overallGrade
has no parameter anymore. So that
means no variable named r
to use. That's okay though, because now this
function is a class member, so it can access the calling object's private
data by name alone.Compile and test the program to be sure it works correctly after all your changes. Fix any problems before proceeding to Step 5.
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
Each pair of students must accomplish the following to earn full credit for this lab:
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.
Quizzes: 90 percent Midterm: 40 percent Final: 90 percent Grade is BBut wait ... think about where you should make these changes. In
main
?
Sure, that could work, but only if main
knows the maximum quiz and exam scores.
That information is actually ... where? Those maximum values, as well as the percentages
used to calculate the overall grade are all buried in the implementation of function
overallGrade
. Oops, this was a poor design decision, because it is not
flexible. What would it take to make it right?
overallGrade
be changed? What change(s)
should be made to main
?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.