Вы находитесь на странице: 1из 4

Name/PID:______________________________

CS2400
Laboratory Assignment #12 and Answer Sheet
Writing classes with constructors
(60 points)
1 Introduction
In this lab, you are going to get some more practice with writing classes with constructors
and passing stream parameters as arguments to a member functions etc.

Please write your answers by hand in the space below:


Given the class definition below:
class Counter
{
public:
Counter ();
//initializes the counter value to 0.
Counter(int new_val); // value is set according to the
// incoming argument.
void increment(); //increment counter value by 1.
int get_value(); //returns the value of member
//variable
private:
int value;
};
a. [5] Write the definition for the default constructor.

b. [5] Write the definition for the constructor with one argument.

c. [5] Write the definition for the accessor function.

d. [5] Write the definition for the increment function

e.[5] Write a short program (main function) to do the following. Create Counter
object with a value you choose (hint: use the constructor with one argument. Also look at
BankAccount class on pg 571-573) If the value is less than 10 increment the value
by 1. Print the original value and the incremented value.

2. Laboratory Preparation
Create a directory call Lab12 inside your 2400/Labs directory.
f. [10] Now type the above program. Save this program as lab12a.cc. Compile and
run the program to check whether you are getting the right output. If it is not correct,
correct the program before submitting electronically. The output of this program can be
Value at the beginning 5
Value at the end 6

3. [25] Tollbooth class - Write a complete program.


Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps
track of the number of cars that is gone by, and of the total amount of money collected.
Model this tollbooth with a class called tollbooth. The two data items are type int
to hold the total number of cars, and type double to hold the total amount of money
collected. A constructor initializes both of these to 0. A member function called
payinCar() increments the car total and adds 0.50 to the cash total. Another
function called nopayCar(), increments the car total but adds nothing to the cash
total. Finally a member function called display(ostream& fileout) to displays
the two totals to the screen. (Look at BankAccount class pg 571-573)
Make the stream parameters versatile (pg 346 347)
Include a main function to test this class. The program should allow the user to enter
p to count a paying car, and n to count a nonpaying car and q should cause the
program to print out the total number of cars and total amount collected . Save this
program as lab12b.cc
Sample output of this program can be as follows (user input in italics):
P
P
P
P
P
P

paid
paid
paid
paid
paid
paid

N
N
N
N
N
N

Not
Not
Not
Not
Not
Not

paid
paid
paid
paid
paid
paid

Total number of cars 5

Q
Q
Q
Q
Q
Q

Quit
Quit
Quit
Quit
Quit
Quit

->
->
->
->
->
->

p
p
n
p
n
q

Total amount collected $1.50


4. Submit the programs electronically as lab12. Also print your programs and attach to
the lab answer sheet. Dont forget to do the documentation.

Вам также может понравиться