Skip to main content

Header files

By January 15, 2020No Comments
“Header files, or .h files, allow related function, method, and class declarations to be collected in one place.”. This is useful as you can group together specific functionality and then export and use it at another place/file to be used by another program. There are 2 file extensions that we deal with “.h” and “.cpp”. The header file will have a “.h” extension whereas, the corresponding functions, methods, and classes will be contained within files that will have the ‘.cpp’ extension. You will include the ‘.h’ or header file in the ‘.cpp’ file with:
/*include header file which is in the same directory as main.cpp
notice that if we in include a header file written by a programmer we use "" and not < >
#include "header_file.h";

2 Types of header files:

  1. System header files: Comes with the compiler
  2. User header files: Written by the programmer

Header file structure:

The header file contains some pre-processor directives:
  1. #ifndef: stands for “if not defined” for example : #ifndef HEADER_FILE_H
  2. #define: So if the header file is not defined, then define it
  3. #endif : End the if statement


// This file will be saved as "header_practice.h"
//we need this as the function declaration needs it
#include <vector>
using std::vector;

int IncrementAndComputeVectorSum(vector<int> v);
void AddOneToEach(vector<int> &v);


Leave a Reply