Files

Can someone explain to me what is a header file with examples to understand it better
you mean like a custom header file (one you write to initiate all the functions and variables of a program you write) or like a header that comes within the C++ database?
Hi :) It seems that your post lacks informations about what you want, but this is how I understood your question.

Maybe you are asking how to make a custom header file.

Im going to show you a simple "Hello World Program" that uses a header file to contain a function.

The simple "Hello world" program comes in this form:

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}


using a function, it will be in this form:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <iostream>

using namespace std;

void hello(void)
{
    cout << "Hello world!" << endl;
}

int main()
{
    hello();
    return 0;
}


using a header file to contain the function it will become like this:

main.cpp listing:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

#include "hello.h"

int main()
{
    hello();
    return 0;
}


hello.h listings:
1
2
3
4
void hello(void)
{
    cout << "Hello world!" << endl;
}


I hope this post helped :)
Last edited on
About twelve posts down from this one there is one named "Question?" that asks about "include <???>" stuff and what it is for. Check out the response there.
Topic archived. No new replies allowed.