Header Files in C++

Header Files in C++
 


So header files in C++ are files which contain code such as classes, functions, structs.... 

In every C++ program we have seen the "iostream" header file, this header file contains objects like cin and cout which handle input and output respectively. There are many standard library header files such as iostream. Others are the user defined header files which we make to have our own code.

The benefit of a header file is that once we write, for example a 100 line code and we need that code in many of your programs, (just as to use the cin and cout objects we use the "iostream" header) it is cumbersome to write that 100 line code again and again, so we  write that code in a header file and just use the header file to get the 100 line code in each of our programs.

To use the header file, we write "#include", which is a preprocessor directive that will first process the code in the header file and then the code in the file in which the header file is used.

Making a Header File

In C the header files end with the .h extension, in C++ the standard library files don't have the .h extension but the user defined header files in C++ have to have the .h extension. We can use the C libraries in C++ by excluding the .h extension and putting a 'c' before the library name. for example - <stdio.h> in C becomes <cstdio> in C++.

Let's look at a basic example of a header file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 // demoHeader.h
 // A header file

 #pragma once		// preprocessor directive to include the file only once
 using namespace std;
 // function to convert lowercase letters of a string to uppercase

 string low_to_up(string str1) {		    // function header
	 for (int i = 0; i != str1.size(); ++i) {   // using the traditional for loop 
						   // to go through each character in the string.
		 str1[i] = toupper(str1[i]);	   // Making each character uppercase
	 }
	 return str1;				   // returning the modified string
 }

On line 3, 


#pragma once	// preprocessor directive to include the file only once

"#pragma once" is a preprocessor directive, which tells the compiler to include the code of the header file only once. This is necessary to not repeat the code. Repeating the code may cause errors and to avoid this, we use "#pragma once". 

Then from line 8, we define a function named low_to_up which returns a string and takes a string parameter.


string low_to_up(string str1) {		// function header

Line 9 states the for loop header,


for (int i = 0; i != str1.size(); ++i) {    // using the traditional for loop to go 
					    // through each character in the string.

The for loop traverses through all the characters in the string, each character is converted to uppercase on line 11.


str1[i] = toupper(str1[i]);		// Making each character uppercase

When all the characters are finished processing, the for loop ends and then, we return the modified string.


return str1;	    // returning the modified string

That was the header file, now we write the main cpp file,

Including the header file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 // program.cpp

 #include<iostream>				// standard library header file
 #include "demoHeader.h"			// user defined header file

 using namespace std;

 int main() {
	
	 string text = "abcdefghijklmnopqrstuvwxyz";	// Initializing a string variable
	 cout << text << endl;
	 text =  low_to_up(text);	// assigning the return value of the low_to_up function
					// to the variable name
	 cout << text << endl;	    // printing the modified value.

	 return 0;
 }

At line 3 is the standard library file (<iostream>) and at line 4 is the use defined header file which we just made.


#include<iostream>		// standard library header file
#include "demoHeader.h"		// user defined header file

The name of the header file should be in double quotes, preceded by the preprocessor directive "#include". 

On line 10, we define a string variable named as text, which is assigned a string literal.


string text = "abcdefghijklmnopqrstuvwxyz";	// Initializing a string variable

On line 11, we simply print the value of variable text.

On line 12, we assign to text the result of the call to the function low_to_up, with text being its parameter. The function converts the string to uppercase and assigns its return value to text, then again on line 14, we print the value of text.

The result is

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

So, we wrote the function in the header file and just used the header file in the main cpp file with the help of the preprocessor directive. Like wise if we want to use that function in any other program we make, we don't have to write the full function code, just have to use the header file.

Comments