23.4 Headers

I l @ ve RuBoard

Information that is shared between modules should be put in a header file. By convention, all header filenames end with ".h". In the infinite array example, we use the file ia.h .

The header should contain all the public information, such as:

  • A comment section describing clearly what the module does and what is available to the user

  • Public class declarations

  • Common constants

  • Public structures

  • Prototypes of all the public functions

  • extern declarations for public variables

In the infinite array example, more than half the file ia.h is devoted to comments. This commenting is not excessive; the real guts of the coding is hidden in the program file ia.cpp . The ia.h file serves both as a program file and as documentation to the outside world.

Notice that there is no mention in the ia.h comments about how the infinite array is implemented. At this level, we don't care how something is done, just what functions are available.

Example 23-3. ia/ia.h
 /********************************************************  * definitions for the infinite array (ia) class        *  *                                                      *  * An infinite array is an array whose size can grow    *  * as needed.  Adding more elements to the array        *  * will just cause it to grow.                          *  *------------------------------------------------------*  * class infinite_array                                 *  *    Member functions                                  *  *      infinite_array(  )  -- default constructor        *  *      ~infinite_array(  ) -- destructor                 *  *      int& operator [](int index)                     *  *              gets an element of the infinite array   *  ********************************************************/ #include <string.h> // number of elements to store in each cell of the infinite array  const unsigned int BLOCK_SIZE = 10;          class infinite_array {     private:         // the data for this block          int   data[BLOCK_SIZE];                // pointer to the next array          class infinite_array *next;     public:         // Default constructor         infinite_array(  )          {              next = NULL;             memset(data, ' 
 /******************************************************** * definitions for the infinite array (ia) class * * * * An infinite array is an array whose size can grow * * as needed. Adding more elements to the array * * will just cause it to grow. * *------------------------------------------------------* * class infinite_array * * Member functions * * infinite_array( ) -- default constructor * * ~infinite_array( ) -- destructor * * int& operator [](int index) * * gets an element of the infinite array * ********************************************************/ #include <string.h> // number of elements to store in each cell of the infinite array const unsigned int BLOCK_SIZE = 10; class infinite_array { private: // the data for this block int data[BLOCK_SIZE]; // pointer to the next array class infinite_array *next; public: // Default constructor infinite_array( ) { next = NULL; memset (data, '\0', sizeof(data)); } // Default destructor ~infinite_array( ); // Return a reference to an element of the array int& operator[] (const unsigned int index); }; 
', sizeof(data)); } // Default destructor ~infinite_array( ); // Return a reference to an element of the array int& operator[] (const unsigned int index); };

A few things should be noted about this file. Everything in the file is a constant definition, a data structure declaration, or an external declaration. Any code that is defined is inline. No actual code or storage is defined in the header file.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net