4.3 Organizing the Code




The code that defines, implements and uses classes is organized as a collection of files. Some ways of organizing these files works for a very small number of classes but do not work effectively for medium and large number of classes. The organization that is described below works for all cases and is the pattern universally used in practice.

The definitions of classes are placed in "header" files while "code" files contain the code that implements or uses the classes. By convention, the header files are named with a ".h" suffix. The conventional suffix for code files varies with the compiler or system. Typical code file suffixes are" ".C", ".cc", or ".cpp". The base part of a file's name is, of course, chosen to reflect the nature of their contents. Which code file suffix is used is not important - what is important is using the same suffix consistently.

Each header file contains one class definition or several highly related class definitions. For example, the simple WindowFrame class, might be placed alone in a header file named "WindowFrame.h". In a windowing system there might also be separate header files giving the class defintions for buttons, sliders, pens, and other paraphenalial typically found in graphical user interface systems. In many cases there are groups of class definitions that are closely related and are grouped together in one header file. Such grouping happens in one of two cases. First, the definition of a window class may involve the use of other classes, in which case these other classes may also be defined in the same header file. Second, the designer of a set of classes may believe that the common usage a user may want an entire set of class definitions; putting them in one header file makes it easier for the user since the user need only examine on header file to find all of the related classes. For example, a graphics system might provide classes for rectangles, circles, colors, rulers, etc. A single header file, say named "Graphics.h", could be used to hold all of these definitions. In organizing the header files, it is important to be sure that only highly related class definitions are put in the same file.

The code implementing a class is placed in its own code file. For example, the code implementing the FrameWindow class might be placed in the file "FrameWindow.cc". Even if several classes are defined in the same header file, their implementations are placed in separate code files.

Header files are "included" in other files that refer to the class(es) defined in that header file. The directive #include "filename" is used to include a file. For example, suppose that the Frame class is defined in the header file Frame.h. A portion of the file using the Frame class might be:

#include "Frame.h"

...
Window display(Location(10, 10), Shape(100, 200));
display.moveTo(Location(50, 50));
...
Notice that the quote marks around the file's name are required. Common cases of including header files are:
Notice that code files do not include other code files, the code only depends on the definition of a class not on that class' implementation.


Next Stop


Last updated: September 6, 1995 / kafura@cs.vt.edu