C++ | Creating Your own Header Files

Introduction.

This is a tutorial for creating  “.h” header files in C++ and using them in your program. The program is given below that uses a header file to add numbers. The program is extendable. Go enjoy the program. Lets begin….

Program for using header files in C++.

  • abc.h file
#ifndef ABC_H
#define ABC_H
 
//the function here
void func(int a)
{
 cout<<endl<<"Your Answer = "<<a;
}
 
#endif
  • The C++ program file.
#include<iostream.h>
#include<conio.h>
#include "abc.h"
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b,c;
//Input the two numbers save them in 'a' and  'b'
cout<<"Enter the no."<<endl;
cin>>a>>b;
//calculate sum and save them in 'c'
c=a+b;
//show the output c
func(c);
//get character
getch();
}

Output

Enter the number
1
2

Your Answer = 3

How does it work.

  1. You enter the numbers.
  2. The numbers are added
  3. The numbers are printed using the function declared in header file.

Extending it.

The program is extendable. This is a basic concept of C++ so it can be used in any program.

Explanation.

  1. Declare the header file.
  2. Include ‘iostream.h’ and ‘conio.h’ files and your header file.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare variables.
  6. Add cout and cin to get numbers.
  7. Add the numbers
  8. Print the numbers using function from header file.
  9. Get character.

At the end

You learnt creating the C++ program for Creating and using header files in C++. So now enjoy the program.

Please  share it.

Sharing options: