15
This homework assignment extends the inclass exercise from Friday (January 28) to implement a
Counter class. Recall the specification given in the exercise: an object of the Counter class is used to count things, so it records a count that is a nonnegative whole number. The operations of this object are:
●
●
●
●
Default constructor that sets the counter's value to 0.
Increment a member function that increases the value of the counter by one.
Decrement a member function that decreases the value of the counter by one. Note that the counter value should be nonnegative, so this function should not perform the operation if doing so would make the counter negative.
GetValue a member function that returns the current counter value.
There is only one attribute:
● value an integer that is the current value of the counter
For this homework assignment, the following additional operations are added to the specification:
●
●
●
Reset a member function that sets the counter's value to 0.
SetValue a member function that receives a number that the counter's value is set to.
operator<< a friend function that receives and passes back an output stream as the left operand and receives a Counter object as the right operand. This function should output (only) the counter's value to the stream.
Complete the Counter class implementation to include the new operations in files counter.h
and counter.cpp
. In addition, modify the default constructor into an explicitvalue constructor that receives the initial value of the counter with a default argument value of 0 (revised 02/01) . (That is, there will still be only one constructor.)
Add tests for the new operations in the main program in the inclass8.cpp
file. The makefile
Makefile.inclass8
will not change from Friday's exercise, but must be correct. (I.e., it must create an executable named inclass8 .)
Electronically submit a tarfile containing counter.h
, counter.cpp
, inclass8.cpp
, and
Makefile.inclass8
as explained in the handout Submission Instructions for CS 215 . The
Revised to fix typo: 02/01/2011 Page 1 of 2 D. Hwang
submission system will run the makefile you provide, but will check only that the executable inclass8 is created. This program will not be run. Then the submission system will compile and link the Counter class against a test program written by the instructor. The test program will be run and will generate output that will be included in the email sent by the submission system. Note that the names of the files must conform exactly as specified, otherwise they will not compile and/or link with the test program.
The submission system will start accepting submissions for this assignment no later than Wednesday evening.
Revised to fix typo: 02/01/2011 Page 2 of 2 D. Hwang