How to Create a C++ Header File
The C++ programming language allows you to organize your code into separate, reusable files. The "header" file contains forward declarations of classes, functions, variables and other components. Header files typically have a ".h" or ".hpp" file extension. The implementations of objects declared in the header file are defined in a separate file, which typically has a ".C" or ".cpp" file extension. This approach allows you to compile your code as a reusable library, using the header file a the library's reference. The header file usually contains the formal Application Programming Interface (API) documentation describing the library for other programmers.
Instructions
-
-
1
Create a new file in your favorite code or text editor. Save it as a plain text file with the file extension ".h." The standard convention is to name your source code file for the class you are developing. For example, if your class is "MyClass," name the file "MyClass.h."
-
2
At the top of your file, add the following lines, replacing the token "MYCLASS_H" with the name of your header file in all capital letters, using an underscore (_) in place of the period (.). For example, if your header file is named "Controller.h," use "CONTROLLER_H." These lines are compiler directives---also called "preprocessor directives"---that prevent the header file from being included twice. The convention of using the name of your header file as the token helps ensure unique tokens are defined for each of your header files.
#ifndef MYCLASS_H
#define MYCLASS_H#endif
-
-
3
Add the #include compiler directive to include any external files your class requires after the #define line and before the #endif line. For external libraries---for example, the Standard Template Library---located in your programming environment, use angle brackets (<>) around the file name (the ".h" on the file name is optional); for example:
#include <iostream>
#include <math>For files in your project directory, use double quotes (") around the file name. For example, to include the Cat.h and Dog.h files for your Cat and Dog classes:
#include "Cat.h"
#include "Dog.h" -
4
Write your class declaration, after the list of included files and before the #endif. Define the member variables, but do not add the constructors, destructor or member functions yet. For example:
class MyClass {
private:
int count ;
float size ;
} -
5
Add your class constructor, destructor and member function prototypes to your class. Function prototypes include the return type, function name, and parameters, but not the function definition. Continuing the previous example:
class MyClass {
private:
int count ;
float size ;
public:
MyClass() ;
MyClass(float s, int c) ;
float getSize() ;
void setSize(float s) ;
} ; -
6
Document your code in comments above each object, describing the class and each variable and function. Save your file.
-
1
Tips & Warnings
Write your function definitions in a separate file. The convention is to give the definition file the same names as the header file, except using a ".cpp," or similar, file extension. In the definition file, you will include the header file and define each function, prefixing the function name with the class name followed by two colons (::). For example, your definition file might look similar to this:
#include "MyClass.h"
MyClass::MyClass() {
count = 1 ;
size = 3.14 ;
}
MyClass::MyClass(float s, int c) {
count = c ;
size = s ;
float MyClass::getSize () { return size ; }
void MyClass::setSize (float s) { size = s ; }
References
Resources
- Photo Credit woman hands typing on laptop keyboard image by .shock from Fotolia.com