CS 2005, B Term 1999
Techniques of Programming
Lab 0 (Oct. 27)
This introductory lab session will help you become familiar
with certain aspects of lab protocol.
You will also see an example of C++ classes and use the g++ compiler.
- Sign in with the TA. You should both print your name
and sign the sheet.
- Listen to the TA's mini-lecture.
- Do the problems listed below. Feel free to ask the TA
questions about the problems. Don't worry if you run out of
time before you fully understand/finish all of the problems.
You're not being graded on this material yet, and further
discussion will be provided in class this week.
The following problems use the Clock class.
Each object of type Clock represents an ordinary clock
that can be set to any time of day in the standard
12 hour:minute am/pm format. The time on a given
Clock object can also be moved forward by a given
number of minutes.
Read the C++ header file clocks.h,
containing a specification of the Clock class.
Notice that the functionality described in the header file
is not implemented in clocks.h. Nonetheless, sufficient information
about the behavior of objects of type Clock is given in the
header file so that the the user can write programs that use them.
Examples of the appropriate C++ syntax to be used in such programs
are given below.
- To define an object named myclock of type Clock:
- To set the time on someclock to 12:35 pm:
someclock.set_time(12, 35, true);
- To move the time on someclock forward by 10 minutes:
Write C++ statements that achieve the following objectives:
- Define an object named myclock of type Clock
- Set the time on myclock to 10:50 am
- Move the time on myclock forward by two hours
Log onto your CCC Unix account and change the directory
Copies of both the specification file clocks.h and a separate
implementation file clocks.cxx for the Clock class
are available in that directory.
You will also find a program that uses the Clock class, in the
file clocktest.cxx (the details of this program don't concern us today).
In order to produce an executable program from clocktest.cxx,
you should perform the following steps:
You may type man g++ at the Unix prompt for information about
compiler options and details.
- Copy all the above files to your own personal directory.
- Compile the implementation file clocks.cxx by typing
g++ -c clocks.cxx
at the Unix prompt. This produces an object file clocks.o
but does not produce an executable file.
- Compile the test program clocktest.cxx by typing
g++ -o clocktest clocktest.cxx clocks.o -lm
at the Unix prompt. This compiles the test program, links the
Clock class implementation to it, and produces an executable
file named clocktest.
Run the executable file clocktest. Try it out using different options.
Examine the file named makefile in the directory /cs/cs2005/.
This file contains the required compiler/linker commands as described above,
as well as a description of the relevant dependencies between the
various files involved. Typing make at the Unix prompt
causes the commands contained in the makefile to be carried out
(you may want to remove some of the object and executable files
before trying this).
See the CS2005 homepage for links to
further information about Makefiles.
If you have enough time left, write your own test program
myclocktest.cxx that uses the Clock class in some way.
For example, your program could create an object of type Clock,
then proceed to print the time on the clock at intervals of 15
minutes, for a total period of, say, 12 hours. Compile your
program and link it to clocks.cxx as described above.
Then try it out by running it!