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() { ... }
#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.
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.ccThe 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.cccreates 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 tYou can build the first target in the makefile by just typing command
makeNormally, you put the target that you most often build first, so that you can use this abbreviated command.