C++ : Including Files

 

You can include other files using the include statement

How to Include

The preprocessor allows you to include the contents of one file in another. This is performed using the #include statement.

  • OPTION 1: If the name of the file to be included is enclosed in angle brackets < >, the compiler will search a standard list of directories for the file you wish to include; you can add entries to this list with the compiler flag -I.
  • OPTION 2: If the name of the file is in quotes " ", it will search the current directory for the files in addition to the other directories.
#include <math.h>
#include <stdio.h>
#include "MyHeader.H"


When to include header file of a class

 

The following is a non-exhaustive list of times when you will need to include the header file for a class:

  • when you create an instance of the class
  • when you call a member function on an instance of the class
  • when you access a public instance variable on an instance of the class
  • when you access a static function or member of the class
  • when you define methods for the class (i.e. Foo.C must include Foo.H)
  • when you declare a member instance in the class declaration
  • when the class you're declaring is a subclass of the class (i.e. SubClass.H must include SuperClass.H)
 

 

© Lynne Grewe