headers

Hi.I am starting to write projects with separated files using .h and .make. However, I am very confused in the usage of headers. Can someone give me a guideline of what i should include in a .h and what should i include in the .cpp?
Thank you
.h
A .h file is a C header file. Typically you should put function prototypes and global variables in a header file (you shouldn't really use globals but if you do, and want them to exist in more than one file, you need to put them in the header file. If you include the header file you need header guards* and global variables will need to be static). Anyway; a header file generally contains preprocessor macros (if any), function prototypes (if any) and global variables (if any).

example:
stuff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _STUFF_H_
#define _STUFF_H_ // *This is a header guard; it protects against the file being included more than once

#include "file_i_want_in_all_my_source_files"
#include <iostream>

static int globalInt; // This is a static int to avoid errors produced by the compiler relating to the variable being defined in each file
const int constGlobalInt = 1; // This is constant; and is better than the above. The problem is that it can't be changed.

#define MACRO std::cout << "MACRO!"; // This is a preprocessor macro

int main(int, char**); // This tells any files that include this header that a function called 
                       // main which returns int and takes two arguments, int and char**.

#endif // _STUFF_H_ not defined 
Last edited on
Take a look at this article as well for even more detail.
http://cplusplus.com/forum/articles/10627/
Topic archived. No new replies allowed.