Separation of Declaration and Implementation
One of the most important constructs of the C/C++ programming language is the class. Classes allow programmers to define datatypes, as well as the functions that interact with the datatypes. Amongst other benefits, this allows us to control the way that data is accessed and modified by the user of the class, ensuring that only valid data is accepted.
When writing our precious classes, we follow a practice of dividing the class into two files: the declaration file and the implementation file. All the information that the class requires and all the code that defines the functionality of the class is present in these two files. The code that utilizes the class appears in the so-called "driver" file - the file where the different parts of the program are utilized and interact in the required manner. This is also typically where the "main" function appears.
The Declaration File
The declaration file, which has a ".h" extension, contains the following components of a class:
  • The class declaration, including class function prototypes and class data members
  • Header files (".h" files) that the class utilizes
  • Constants or other global variables used by the class
  • Friend or other global functions that the class utilizes or provides
In simpler terms, the header file contains everything except the implementation code for each of the functions of the class. The class declaration is always present in this file (don't forget to end it with a semi-colon), as are any libraries that the implementation file will use. The implementation file makes use of the declaration file as more or less of a reference for what the implementation it contains is referring to.
The Implementation File
The implementation file, which has a ".cpp" extension, contains the implementation for each function defined in the declaration file of a class. This is where the libraries defined in the declaration file are utilized, as are any global variables, constants, or other special data declared in the declaration file, by the functions of the class. The code for all functions in the class should be located in this file.
The implementation file must contain a reference to the declaration file so that the functions defined in the declaration file make sense to the compiler and other segments of the program. To create the reference to the declaration file, use the following statement, where declaration represents the name of the declaration file, which should be the same as the name of the class it contains:
#include "declaration.cpp"
Please take note of the fact that the filename of the declaration file is enclosed within quotation marks, not angle brackets. If files are not included in this way, the compiler will look for your class files in the location where standard C/C++ libraries are located. Since this is not where your class files are located, you must use quotation marks to indicate that the files you are including are in a specific location. Within the quotation marks, you can specify the full path to the file you wish to include. If jus tthe filename is specified, it is assumed that the file resides in the same directory as the current (implementation) file.
The Driver File
The driver file is where the program logic is located. That is, where the actual function and user-interaction of the program are defined. While classes contain infromation about datatypes used by the program, the actual program structure that defines how these datatypes interact is located in the driver file.
Typically, the driver file contains the "main" function for the program, as well as any other miscellaneous functions that the program uses that are not a part of other classes. In order to use the classes that are defined in other files, it is necessary to include a reference to the implementation file for the class in the driver file, or anywhere else that the class is used. To create this reference, place the following statement at the beginning of the driver file:
#include "Class.cpp"
Here, the tag Class refers to the name of the class implementation file that the program will utilize. You must include one of these references in any file that makes use of the class you wish to utilize.
Naming Class Files
The declaration and implementation files for a class have ".h" and ".cpp" extension, respectively. The parts of the filename the precede the extensions should be the same as the name of the class. Thus, if we make the class "MyClass", it will have a declaration file named "MyClass.h" and an implementation file named "MyClass.cpp". While this is not required, this is the convention for C/C++ programs as it makes teh contents of each file obvious from just the filename.
Using Classes
In any class or program file that utilizes another class, it is necessary to place a reference to the file containing the class implementation at the beginning of the file. That is, if one wishes to use the Stack class in a certain other class or, for example, a driver file, one must include the following statement at the beginning of the file:
#include "Stack.cpp"
This command instructs the compiler to look in the file "Stack.cpp" for information about the Stack class. Again, note the use of the quotation marks around the filename, which instruct the compiler to look in the current directory (if no absolute path is specified) for the file. The use of the angle bracket will result in a compiler error.
The "#include" statement can be thought of as instructing the compiler to instantly insert the contents of the specified file at the clocation where the statement occurs. Thus, when the declaration file is referenced at the beginning of the implementation file, it is equivalent to placing the contents of the declaration file at the beginning of the implementation file. By separating the class information between files in this manner, the class information remains organized, yet coherent.
Compiling
In order to compile a simple program consisting of a class (two files, one ".h" and one ".cpp") and a driver (say, "main.cpp"), at this point we can issue the simple command:
g++ -o testProgram main.cpp
This will compile the program, giving the executable the filename "testProgram". Because of the way that the class implementation file has been included, the contents of the implementation file will be included at the beginning of the driver file. The "inserted" implementation file will include the information in the delcaration file for the class, in addition to the implementation for the class. This is a simple, daisy- chain way of linking class files into programs. While this method is effective, it will result in compiler errors in more sophistocated programs, or often when using makefiles. Therefore, it should only be used in smaller programs that involve one or two classes and a driver file.
On This Page...
The Declaration File
The Implementation File
The Driver File
Naming Class Files
Using Classes
Compiling