What are header files and how to use them

You may have noticed that in coding a C programing, there are files that are included at the top of a program, #include statement or even commands you write. You also might have noticed that these commands are written just like other codes but carry out different functions unlike other codes. Indoducing Header files.

 

What are header files and why we need them:

Header files can simply be defined as a file imported into a  program using the pre-processor, #include statement. It is also known as an included file whose purpose is to hold declarations for other files to use. Lets take a look at a few programs and find out how they work.

 

Basic “Hello World” program:

#include <iostream>
using namespace std; 
  int main()
{
cout<<"Hello World"<<endl; 
return 0;

This program prints “Hello World” to the console using “cout”. However, this program never defines cout. This reason is because “cout” has already been declared in a header file called “iostream” which was included.

Writing our own:

C++ programs take the form of collecting separately compiles modules. These modules consist of structure type declaration, class declaration, global variables and functions. These functions are defined in a source file(ie, a .cpp file). Each source file has a header file(ie, a.h file) associated with it that provides the declarations needed by other modules to make use of this module. That is, other modules can access the function of module “A” simply by including the A.h header file, and the linker will do the rest.

Header files are highly needed in a code, precisely c++ because it tells the programmer how to use the library correctly and by the use of header files, the program can access a particular function.

//in A.cpp
class A
{
public:
   void doSomething();
 int bar;  } //in main.cpp int main() { A something; //A is unidentified therefore, there's compiler error return 0; }

 

Error is given when  main.cpp is compiled because “A” is declared in the program but not in main.cpp . Header files allow the programmer to make the class A interface visible to other .cpp files while keeping its implementation in its own .cpp file.

 

//in main .cpp
#include A.h  //it defines A
int main()
{
A something;  //no error as A is defined 
 return 0;
}

 

 

The #include statement is just like a copy and paste operation so the compiler will replace the #include line with its file contents you are including during its compiling process.

 

//Assuming they were no header files; 
//in add.cpp
int add (int x, int y) 
{
return x+y;
}

//in main.cpp
include <iostream>
int add(int x, int y);  //foward declaration using function prototype 
using namespace std; 
int main()
{
cout<<"the sum of 3 and 4 is"<<add(3,4)<<endl; 
  return 0;
      }

 

 

The above program can be written easily with the use of header file as;

 

//With header file 
//add.cpp remains the same 
     //in main.cpp
include<iostream>
include "add.h"
using namespace std; 
int main()
{
cout<<"the sum of 3 and 4 is"<<add(3,4)<<endl; 
return 0;
}

 

HOW TO USE A HEADER FILE:

Below are guidelines you must follow on how to use a header file;

  • Each group of declaration and functions(module) with its .h and .cpp file should correspond clearly to its functionality.
  • Always include guards in a header file because these are techniques that use the standard pre-processor features to avoif one header file being included multiple times. The most compact form uses “ifindef”. Always choose a guard symbol based on the header file name and make the symbol ALL-CAPS. Example;
//this is the start of the header guard
#ifndef A.h
#define A.h
 class A
{
  public:
    void doSomething();
};
#endif 
//this is the end of the header guard
  • Include all the decorations needed. Use a module in its header file because this file is always used to access the module.
  • No namespacing statements are allowed at the top level in a header file.
  • Keep modules internal declarations out of header file. That is, if you need class or struct declarations, global variables, templates or functions that are used only in the .cpp file of the code, and do not mention them in the .h file.
  • The .cpp file should first include its .h file and any other header required for its code.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.