Lab 3. Putting functions inside
classes
This time I am giving you a partial
program which is a C++ version of the
last one. Your task is to complete the program by writing the bodies for the
three member functions of the class Picnic so that the program behaves
just like the last one. i.e. it prints the number of hot dogs per child
and the number of left-overs. The only changes are to split the tasks
between the three functions 'input', 'calculate' and 'output', and to
add a test to prevent division by zero.
Follow these steps to complete the
assignment.
- Get the source code
exactly as in your first and
second assignments.
- Use the editor to add bodies for the
Picnic member functions 'input', 'calculate'
and 'output' to print out the corresponding result.
- Insert in 'calculate' a test for the
number of children being zero, and
in that case, make the program print a message instead of doing
the operation. 'output' also needs a test to stop it from displaying its
message.
- Compile and run the program several
times to test its results.
- Run the program twice more with the
these inputs:
First run: number of hot dogs: 23, number of children: 7
Second run: number of hot dogs: 23, number
of children: 0
- Redirect the output to a file in
each case, and insert the outputs into a copy
of the source code file. You can also redirect the input by typing the
numbers into a file and using:
% a.out < in > out
- where 'in' is the name of this input
file, and 'out' is the name of the output
file.
- Insert the contents of the 'out'
file into the source code file, print it on th elineprinter and hand it to
me (RTH).
- Use the assignment
submitter to submit the source code for lab3.
Points to note:
- You may get compilation errors
and/or warnings. Pay attention to these messages
because you program may not run correctly, or at all because of
these errors.
- Don't forget to put your name in the
file comment block.
- The three functions are all 'void'
functions--they do not return any
value.
- You may need extra variables to hold
the results of the arithmetic operations.
You should declare these also in the private data section of
the class, along with the variables that hold the input values.
- Use the if-else form in the test for
zero. The 'then' branch will be
your error message, the 'else' branch will be the arithmetic operation(s).