This lab will give you an opportunity to write a c++ program using
the
usual
multiple file organization. (Examine the examples starting on
p. 95 and p. 105
of the text.) You will take the single file
program OOD2.cpp and rewrite
it as a multiple file program. This rewrite will include:
- .h and .cpp files for the class Time
- .h and .cpp files for the class
Interval
- a .cpp file to hold the function main
While rewriting, note the following:
Class declarations go in the .h file; the member functions appear
there
only as prototypes.
The member function implementations go in the .cpp file.
The function implementations must be named by preceding them with
the
class name and ::
For example, on p. 422, setTime has become Time::setTime.
Otherwise
the compiler
won't know that they are part of the Time class.
The main program is in a .cpp file by itself. main can use the
other classes by doing an
#include statement on the .h file of each class it needs access to.
The .cpp implementation file for the member function of a class
needs
to use an
#include statement
- for its own class
- and for any other classes
it may be using (as parameters, local variables, return types)
We do not need any #include statements applied to .cpp files.
Instead, each .cpp file must
be included in your project (e.g. added to the folder called "Source
Files").
Other #include statements should be added only as needed to satisfy
the compiler.
For example, your .h files will not have any cout or cin, so they don't
need to include <iostream>.
But if a .cpp file uses cout/cin, it will need to include
<iostream>
Sometimes one .h file will need to include another. For
example
if one class (Employee) uses
the class Date to keep track of when the employee was hired, Employee.h
will include
Date.h.
.h files that you write are quoted (#include "MyClass.h";)
.h files that are part of the standard libraries (<iostream>
<cmath>)
use angle brackets.
Use the trick shown on p. 421 to prevent a header file from being
read
twice
A .h file can begin with the lines
#ifndef <NAME_OF_CLASS>_H
#define <NAME_OF_CLASS>_H
and end with the line
#endif
Be sure to replace <NAME_OF_CLASS> with the actual
name
of your class, i.e. TIME or INTERVAL.