Synopsis:
Well designed source code is factored into many parts, some logical and some physical. Logical parts are things like functions, classes, and namespaces. Physical parts are the files and packages that we use to implement a program.
A C++ package usually consists of two files, a header file, like Str.h, and an implementation file, like Str.cpp. Each package should have a single responsibility. The Str package defines facility that allows users to easily manipulate null terminated arrays of characters, e.g., strings.
Each package consists of a Prologue (Package.h) - comments that:
- Define the package responsibily
- Author
- Context in which the package can be built
- Description of the package operations, including brief overview of the package interface
- All the files required to build the package - it may depend on other packages
- A maintenance history, citing each version and the major changes for that version
- One or more class declarations
- Definitions of inline and template members
- Declarations of associated global functions, if any
- Non-template member function definitions
- An embedded test stub - main function with a test for each required behavior
The package header is wrapped in a set of Preprocessor declarations that ensure that the header is only included once in each compilation unit - a cpp file with all of its included headers.
-
#ifndef TEST_PACKAGENAME
#define TEST_PACKAGENAME
...
#endif
The test stub is wrapped in a pair of Preprocessor declarations that ensure that the main is compiled only if the package is being tested in a stand-alone mode.
-
#ifdef TEST_PACKAGENAME
...
#endif
Str Package
The string package is a good illustration of Package Layout and the C++ constructs used to build robust, efficient, and readable code. We will look at Str in detail in the next part of this course.