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:
|
Last updated: September 6, 1995 / kafura@cs.vt.edu