Join super friendly code & developer communities

A place for coders and developers to share, learn and grow together.

C++ | Creating Your own Header Files

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.

P.S. If you have any questions, click here to join our community and feel free to ask any questions.

Share:
Super friendly <br/>code & developer communities

Super friendly
code & developer communities

A place for coders and developers to share, learn and grow together.