CS 210 – Fundamentals
of Programming I
Spring 2007 – In-class Exercise for 3/19/07 &
3/20/07
Name(s):
(20 points) Complete this exercise in pairs. Write the answers to the written part in this assignment sheet and turn it in with a print out the program files when you are done. The purpose of this exercise is to implement a C++ class from a problem statement.
We often want keep track of the number of items we have encountered by counting them. While we can use a plain integer variable for this task, an integer variable does not prevent misuse such as becoming a negative value or setting it to some random value. Instead we would like to have a Counter object that we can use to count things that will prevent misuse.
Define a class for a Counter object. An object of this type is used to count things, so it records a value that is a nonnegative integer. The operations that can be performed on a Counter object are:
Default constructor that creates a Counter object with a value of 0.
Explicit-value constructor with one received parameter that creates a Counter object with its count set to the value specified. If the value specified is less than 0, it should display an error message saying it will create the object with the value 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. In this case it should display an error message to the screen and leave the value unchanged.
Value - a member function that returns the current counter value.
Write - a member function that receives and passes back an ostream object and outputs the counter value to the ostream object.
Specification of Counter class
1. (1 point) What is/are the attribute(s) of a Counter object? Give their type(s).
2. (6 points) Write the analysis and design for each of the Counter class operations:
a. Default Constructor
b. Explicit-value Constructor
c. Increment
d. Decrement
e. Value
f. Output
3. (8 points) Create a new project on your network drive. . A main program file inclass18.cpp that uses the Counter class is available at the course webpage under today's date. Download this file to your project directory. Add inclass18.cpp to your project.
a. Create a new C++ header file (note this is a different choice from a source file) in your project and call it counter (the compiler will automatically give it the ".h" extension). Write the Counter class definition for the Counter class in it. Note that the names of the operations must be as indicated above (including capitalization) so that they match with what is expected by the main program.
b. Create a new C++ source file in your project and call it counter (the compiler will automatically give it the ".cpp" extension). Write the Counter operation function definitions in it. Build your project and run it, until you are satisfied it works.
4. (2 points) Answer the following questions regarding your implementation:
a. What is the difference between the public and private sections of the class definition?
b. How are member function definitions different from "regular" function definitions? I.e., how do you tell the compiler the functions are part of the Counter class?
5. (3 points) Modify the code in the following ways:
a. Add a Clear member function to the Counter class that has no parameters and sets the value of the counter to 0. Write a test for this function in the main program.
b. Add a Reset member function to the Counter class that has one received parameter, the value to set the counter to. If the received value is less than 0, the function should display an error message and ignore the attempt to reset the counter. Write a test for this function in the main program.
When you have completed this exercise, print out all three files and turn them in with one copy of this exercise sheet with your answers to the questions.
03/17/07