Computer Science 2611
Spring 2000
Laboratory Assignment 7

The assignment

Large programs are broken into many files. This assignment is to break up your solution to assignment 6 into separate files. Put the random number generator in an implementation file random.cc or random.cpp, and a header file random.h. Put the part of the program that computes an estimate of pi in a file pi.cc or pi.cpp. There should be a total of three files. Turn in a printout of all three.

Header and implementation files

Each part of a C++ program is called a module. Each module normally consists of two files, a header file and an implementation file. The header file is an advertisement to the other parts of the program, telling what functions and other things this module provides. The implementation file provides the details of how the functions are performed. It is in the implementation file where the things that are provided are created.

To make a class a separate module, put the class into the header file. Do not put the definitions of the functions in the class. Instead, replace each function by a prototype. A prototype is just a function heading followed by a semicolon. For example, a prototype for the sqrt function is

   double sqrt(double x);
The prototype for your function nextRandom will be
  int nextRandom();

Now put the function definitions into the implementation file. Since the function definitions are really part of the class, but you have moved them physically outside the class, you need to tell the compiler that they belong in the class. Do that by putting the name of the class, followed by ::, in front of the function name. For example, the definition of the nextRandom function will look like this, assuming that your class is called Random.

   int Random::nextRandom()
   {
     ...
   }

Including a header file

To include a header file in another program, use a #include directive. Put quotes around the name of the header file. (The quotes tell the compiler to look in the current directory for the header file instead of in the directory where the standard library header files are located.) To include header file random.h, you write
  #include "random.h"
Use this line in any file that needs to use the things described in random.h. Important: Be sure to include random.h in the implementation file random.cc or random.cpp. Each implementation module always includes its own header file.

Compiling and linking

When a program is broken into separate modules, each module is compiled separately. Use a makefile to do this. Create a file called Makefile, and put the following in it. I am presuming that your two implementation files are called pi.cc and random.cc, and that you want your executable file to be called pi.
  COMPILE = g++ -c -Wall
  LINK    = g++

  pi: pi.o random.o
	$(LINK) -o pi random.o pi.o

  pi.o: pi.cc random.h
	$(COMPILE) pi.cc

  random.o: random.cc random.h
	$(COMPILE) random.cc
The lines that define COMPILE and LINK tell what $(COMPILE) and $(LINK) are replaced by. Compiling with option -c creates a file that is not executable, but that is prepared to link to other files. For example, running command
   g++ -c random.cc
creates file random.o. You cannot run random.o, but you can link it into another program. The g++ compiler will link for you, as you can see in the makefile.

Each item that you build (pi, pi.o, random.o) is a target in the makefile. The target is followed by a colon, and then a list of the files that are used in building it. After that is a command (or several commands) with a tab in front of it (or in front of each one, if there are several). The command is used to build the target. It is very important to begin each command line with at tab.

To build a target t, type command

  make t
You can build the first target in the makefile by just typing command
  make
Normally, you put the target that you most often build first, so that you can use this abbreviated command.