Working with Header Files
Tags: cpp, computerscience, beginners, oop
Published: 2020-11-25 10:34:53

---
title: Working with Header Files
published: true
description: Benefits of working with multiple files in C++
tags: cpp, computerscience, beginner, oop
cover_image: https://images.unsplash.com/photo-1457694587812-e8bf29a43845?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxzZWFyY2h8M3x8ZmlsZXN8ZW58MHx8MHw%3D&auto=format&fit=crop&w=900&q=60
series: Programming 101 with C++
---

In theory, a C++ application can be just one giant cpp file. But there would be a **number of problems** with it - 

* requires compiling the entire source code when making any changes
* increases compile time
* difficult to co-ordinate the work of several developers
* difficult to find what you want to change in a 10000 line file.

# Header Files

To use a function implemented in another file, you **must tell the compiler** about it.
* its name, the parameters it takes and its return type

One way to do this is to declare the function at the top of the file

> int add (int, int);

but this becomes tedious.

Long **collections of declarations** in many files has its disadvantages:
* **challenge to maintain** - say you want to change the function name, or add a new parameter to it; you now need to change the declaration in each of the files that have declared this function for use.
* hides the **"more important code"**

With **header files, you put the declarations in a separate file** that is included into each of the files where you want to use these functions.

> As a result, code is cleaner, easier to understand and easier to maintain.

This is done using the *#include* directive.

Anything that starts with **#** in C++ is an instruction to the preprocessor which is a step that runs before the compiler.

When you include a header file, **the preprocessor pastes the entire content of the header file** right where the include statement is written.

So the compiler complies all your source code that is in your cpp file as well as things pasted from the header file together.
* This is called a translation unit.

Changing our [current code](https://dev.to/blender/function-overloading-2co9) to include header files - 

### Header file with function declaration

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kqv3thggjoit8wm3cdl4.png)

The lines above and below the declarations are included by the IDE and can be ignored for now.

### CPP file with function definitions

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/874b0flrxnw7rovmg3yn.png)

### Calling test from main

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p9ue2j9j10paqsfba4p3.png)

One thing to note here is that, when we [included iostream](https://dev.to/blender/include-o63) we used angle brackets.

When we include our own file, stuff from our own project, we use double quotes.

This **affects where the compiler will look for a file** that's included in your source.
* **Angle brackets <>**: external library not part of the project
* **Double Quotes ""**: somewhere in the source and header files for this project

We can make changes in our IDE to change this behaviour, but following this convention also helps others who might read your code in the future figure out what kind of header is internal and which ones are external.

Please leave out comments with anything you don't understand or would like for me to improve upon.

Thanks for reading!