How to create your own header file in C++

Hello, Coders! In this C++ tutorial, we will discuss and learn about the header files in C++ and how to create our own header files easily.

Header Files in C++

  • C++ language has a large collection of libraries of predefined functions. The header files contain various types of predefined standard library functions. It contains the function definition, data type definition, and macros.
  • The predefined functions can be used by simply including the header files with the preprocessing directive #include. By using the preprocessing directive, we can import the functions and use them anywhere in the program.

We can divide the header files into two types:

1. Predefine/System header files: The header files are already predefined or available in the C++ compiler, to use the functions we just need to import them in our program.

2. User-defined header file: These files are defined/designed by the user themself and can be used or imported by using the preprocessor directive #include.

Syntax:

#include <header_filename.h>
//or
#include "header_filename.h"

How to Create a Header file in C++

  • Instead of using the predefined functions, we can create our own header files and import them to any program. It increases the code readability and functionality.
  • Let’s consider a problem where we want to calculate the sum of digits. Since there is no predefined function for the operations in the standard C++ library, we will create it by ourselves.

Step-1:

Open any text editor or IDE and write your C++ code. Then save it with a “.h” extension.

Example: sumdigit.h

int digitSum(int number) {
    
        int sum = 0;
        
        while (number != 0) {
            sum = sum + number % 10;
            number = number / 10;
        }
        
        return sum;
    }

Step-2:

Locate the saved file in your disk and move it to the include directory inside the bin folder of your C++ compiler. In the case of the Linux system, move the header file to /usr/lib/gcc/include directory.

Step-3:

Write a C++ program in your preferred IDE. Before defining the main method, we will import the functions by mentioning the header files to the program. Include our newly created header file as #include<sumdigit.h> or #include”sumdigit.h” along with other header files.

#include <iostream>
#include <sumdigit.h>

using namespace std;

int main()
{
    int digit;
cout<<"Enter a Digit to get the Sum: " <<endl;
cin>>digit;

int sumofdigit = digitSum(digit);

cout<<"The Sum of digit " << digit << " is: " << sumofdigit <<endl;
return 0;

}

Step-4:

Save the file with the “.cpp” extension, compile and run the program to get the sum of the given digit.

Output:

Enter a Digit to get the Sum: 867
The Sum of digit 867 is: 21

Hope this C++ tutorial has helped you to understand the concept of header files and create user-defined header files in C++.

Happy Coding!!

You can also read, Move files from directories in C++

Leave a Reply

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