January 15, 2016

How to make header file in Visual C++


   A header file is a file containing C/C++ declarations and macro definitions to be shared between several source files. Header file has .h extension. The header file is included in program with the preprocessing directive ‘#include’

Header file consist of two parts. The first part is called header guard. Header guards prevent a given header file from being #included more than once from the same file. The second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see.The header file contains only declarations, and is included by the .c/.cpp file for the module. The .c/.cpp file should first #include its .h file, and then any other headers required for its code.




Step 1:


Header File - New Project


Create a new general empty project in Visual C++.  


 Step 2:  

   
Header File - ADD.H



Add a new header file ADD.H.


  Step 3:


ADD.H - Write Code


Write the code in add.h file as shown below:


// Add header guard ADD_H. The name of the header guard should be same as the name of the .h file.

#ifndef ADD_H
#define ADD_H

// This is the content of the .h file, which is where the declarations go.

//The header file contains only declarations, and is included by the .cpp file for the module.
// So do not define functions in header files and do not define variables in header files unless they are constant.

// Function prototype for add.h

int add(int num1, int num2);

// This is the end of the header guard.

#endif // ADD_H



Step 4:
 


Header File - ADD.CPP




Add a new .cpp file ADD.CPP.

Step 5 :
 

ADD.CPP - Write Code




Write the code in add.cpp file as shown below:


#include "add.h" //The.cpp file should first #include its .h file, and then any other

                       //  headers required for its code.

int add(int num1, int num2)
{
          return num1 + num2;
        }


Step 6:



Header File - MAIN.CPP


To use your header file ADD.H in your program add a new .cpp file main.cpp.  

Step 7:
 

MAIN.CPP - Write Code


Now write the code as shown below :

#include <iostream>
#include <conio.h>
#include "add.h"

using namespace std;

int main()
{
      
       cout<<"sum of 5 and 6 is :: "<< add(5,6)<<endl;
       _getch();
       return 0;
}


Step 8:Execute the program by pressing Ctrl+F5.


Step 9:
 


Header File - Output



Review the resulting program window.




Note :

1.    Always include header guards.

2.  The header file contains only declarations, and is included by the .cpp file for the module. So do not define functions in header files and do not define variables in header files unless they are constant.

3.  Give your header file(.h) and source file(.cpp) the same name. For example, A.h goes with A.cpp.

4.  You can also include other header files in your header file. Only include those header files which is necessary.

5.  The .c/.cpp file should first #include its .h file, and then any other headers required for its code.
 


No comments:

Post a Comment