Header Files in C++

Before preceding to header files, you must aware of these terminologies:
Preprocessor directives :
Preprocessor directives are lines included in the code of our programs that are not program statements but directives for the preprocessor. Processors are automatically invoked by compiler. Processing happens before compiling. These lines are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of code begins, therefore the preprocessor digests all these directives before any code is generated by the statements. Directives in the source file tell the preprocessor to perform specific actions. Preprocessor directives can appear anywhere in a source file. #include,  #define,  #ifdef,  #ifndef,  #endif, etc are preprocessor directives.
A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls. example of Object-like macros : #define pi  3.141   Here #define is a preprocessor directive and pi is a macro, whose content is 3.141
Both user and system header files are included using the preprocessing directive `#include’. It has two variants:
#include <file> : This variant is used for system header files. It searches for a file named file in a standard list of system directories.
#include “file”: This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file.
Precutions taken while including file:
1) Only #include things you need to include (covered next section)
2) Guard against incidental multiple includes with include guards.
Question: What are Header Files?
Answer:  A header file is a file that allows programmers to separate certain elements of a program’s source code into reusable files. Header files commonly contain forward declaration of classes subroutines, variablesand other identifier. Header files have extension .h, .hxx, .hpp
Question:  What is its purpose?
Answer: 1) It speeds up compile time. As your program grows, so does your code, and if everything is in a single file, then everything must be fully recompiled every time you make any little change. This might not seem like a big deal for small programs, but when you have a reasonable size project, compile times can take several minutes to compile the entire program. Can you imagine having to wait that long between every minor change?         Compile / wait 8 minutes / “oh crap, forgot a semicolon” / compile / wait 8 minutes / debug / compile / wait 8 minutes / etc
2)   It keeps your code more organized. If you seperate concepts into specific files, it’s easier to find the code you are looking for when you want to make modifications (or just look at it to remember how to use it and/or how it works).
3)  It allows you to separate interface from implementation. If you don’t understand what that means, don’t worry, we’ll see it in action throughout this article. Here interface means that Header files allow you to make the class declaration(interface) visible to other .cpp files, while keeping the implementation of that class( function body) in its own .cpp file.
4) To make your logic secret. You could implement some state-of-the-art, brand-new, useful, revolutionary library, coded privately; compile it, and distribute the binaries and header files for public use and never distribute your top-secret implementation code!
Question: Where we can use it?
Answer: We can include Header Files every in our File. But keep in mind that the processing always done before compilation i.e the lines having preprocessor directives will read first than the source code of a file. In a small program, might be its use neglected. But in a large program, it breaks the large code in smaller codes by seprating the declarations from its implementations.
Question: What are Header Guards?
Answer: C++ compilers do not have brains of their own, and so they will do exactly what you tell them to. If you tell them to include the same file more than once, then that is exactly what they will do, results in errors. Suppose you have a large program containing many files, and unintentionally  you had included header file twice in your program( in seprate files), then during compilation you will got the errors that  the declations done in one file are redeclared in other file. To remove this type of problem, we use Header Guards. Header Gaurds Guard against incidental multiple includes in  a program.
———will update soon————————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s