How to create your own Header File?

Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want. It enhances code functionality and readability. Below are the steps to create our own header file: 

  • step1: Write your own C++ code and save that file with “.h” extension. Below is the illustration of header file:  

C++




// Function to find the sum of two
// numbers passed
int sumOfTwoNumbers(int a, int b) { return (a + b); }


  • step2: Include your header file with “#include” in your C++ program as shown below:

C++




// C++ program to find the sum of two
// numbers using function declared in
// header file
#include "iostream"
 
// Including header file
#include "sum.h"
using namespace std;
 
int main()
{
 
    // Given two numbers
    int a = 13, b = 22;
 
    // Function declared in header
    // file to find the sum
    cout << "Sum is: " << sumOfTwoNumbers(a, b) << endl;
}


Output

Sum is: 35


Header Files in C++

C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the “.h” extension unlike in C, Where all the header files must necessarily end with the “.h” extension. Header files in C++ are basically used to declare an interface of a module or any library.

A header file contains the following: 

It offers the above features by importing them into the program with the help of a preprocessor directive “#include”. These preprocessor directives are used to instruct the compiler that these files need to be processed before compilation. In C++ program has the header file which stands for input and output stream used to take input with the help of “cin” and “cout” respectively. 

Syntax of Header Files in C++

#include <filename.h>  // for files already available in system/default directory
or
#include "filename.h"  // for files in same directory as source file

We can include header files in our program by using one of the above two syntaxes whether it is the pre-defined or user-defined header file. The “#include” preprocessor is responsible for directing the compiler that the header file needs to be processed before compilation and includes all the necessary data types and function definitions.

Example

The below example illustrates the inclusion of header files in a C++ program.

C++




// C++ program to demonstrate the inclusion of header file.
 
#include <cmath> // Including the standard header file for math operations
#include <iostream>
using namespace std;
 
int main()
{
 
    // Using functions from the included header file
    // (<cmath>)
    int sqrt_res = sqrt(25);
    int pow_res = pow(2, 3);
 
    // Displaying the results
    cout << "Square root of 25 is: " << sqrt_res << endl;
    cout << "2^3 (2 raised to the power of 3) is: "
         << pow_res << endl;
 
    return 0;
}


Output

Square root of 25 is: 5
2^3 (2 raised to the power of 3) is: 8

Note We can’t include the same header file twice in any program.

Similar Reads

Types of Header Files in C++

...

How to create your own Header File?

There are two types of header files in C++:...

Contact Us