How to Create a C++ Header File

How to Create a C++ Header File thumbnail
Organizing your C++ source code simplifies managing, building and distributing your software.

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.

Things You'll Need

  • Code editor or text editor
Show More

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.

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 ; }

Related Searches:

References

Resources

  • Photo Credit woman hands typing on laptop keyboard image by .shock from Fotolia.com

Comments

You May Also Like

  • How to Read a CSV File in C

    Many software products that deal with numbers and calculations have the ability to output data into a Comma Separated Value (CSV) file....

  • How to Include a Header File

    Evaluate the "include" syntax. The following is placed at the top of the C code file: #include <sysfile> (system file) #include "custom...

  • How to Install a C5 Header

    The C5 Corvette is a sports car produced by General Motors from 1997 through 2004. Equipped with a LS series V-8 engine,...

  • What Is the File Extension .C?

    Computer programming is the process of creating applications and programs using specialized languages that computers are capable of interpreting. There are a...

  • How to Create PDF Files With C Source Code

    Creating Adobe PDF (Portable Document Format) files with an application written in the C programming language could potentially involve many hours of...

  • How to Make SQL Tables from CSV File Headers

    CSV stands for Comma Separated Values. A CSV file is one of the most common formats for a database file. You can...

  • How to Create a Web site Header

    Because the header is the first thing a user sees when browsing your website, it must reflect your site's style and content....

  • How to Create a Text File Using C++

    Creating text files in C++ programming code is critical for many applications. You may need to build useful features into your C++...

  • How to Create a Class in C#

    If you're an experienced programmer or have some knowledge of Java or C++, it should be simple to create a class in...

  • How to Make Windows Help Files

    Create your own Microsoft Windows help files in the same format that Microsoft distributes using a help file authoring tool. A help...

  • How to Create Custom Web Headers

    A web header is a graphic that helps to bring the overall look and structure of your website together. Without some type...

  • How to Clear Blogger Headers

    Blogger is a site which allows users to create and maintain a blog. The site also gives users the ability to design...

  • How to Change a Class File

    The Java programming was developed and released by Sun Microsystems in 1995. Java programs are written as text files and then compiled...

  • What Is a Radiator Header Tank?

    A radiator header tank is an integral part of a modern automobile's cooling system. It has an important role in dealing with...

  • How to Create a Document File Library Page

    A document file library page is a page of files that share a common theme, such as a project, and are shared...

  • How to Repair File Headers

    When the objects in a ZIP file are inaccessible, this means the ZIP file itself is corrupted. Corruptions in the file system...

Related Ads

Featured