What Should Go into an .H File

What should go into an .h file?

Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, "definitions".

Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, "implementations".

The simplest question to ask yourself to determine what belongs where is "if I change this, will I have to change code in other files to make things compile again?" If the answer is "yes" it probably belongs in the header file; if the answer is "no" it probably belongs in the code file.

What Should go in my Header File in C++?

Header files contain function and class declarations. These simply declare the name of the function, its return type, and its argument list. The .cpp file contains the definitions of these functions -- i.e. the actual implementation. The header file is visible to the rest of the program if you #include it in other files, but the implementation details are hidden in the .cpp file.

Anything declared/defined in the .cpp file that is not in the .h file is not visible to the rest of the program, so you can define internal variables, helper functions, etc. in the .cpp file and those implementation details will not be visible. An example of this is foo() in my example below.

A very rough sketch of your program would look like this:

In prog1.h:

#include <iostream> // and whatever other libraries you need to include

#define ARRAY_SIZE 100 // and other defines

// Function declarations

// Read number from file, return int
void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]);

char assign_char(int n);

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]);

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]);

In prog1.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"

void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]) {
// implementation here
}

char assign_char(int n) {
char c;
// implementation here
return c;

}

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]) {
// implementation here
}

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]) {
// implementation here
}

// not visible to anything that #includes prog1.h
// since it is not declared in prog1.h
void foo() {
// implementation here
}

In prog1_test.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"
// any other includes needed by main()

int main() {
int array[ARRAY_SIZE][ARRAY_SIZE];

read_number(array);

for (int i = 0; i < ARRAY_SIZE; i++) {
for (int j = 0; j < ARRAY_SIZE; j++) {
assign_char(array[i][j]);
}
}

neighbor_check(array);

print_array(array);

return 0;
}

Should struct definitions go in .h or .c file?

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

Public structures should go in the .h file.

.cpp vs .h and where should I put function definitions

You should use .h file for function prototype and data type declarations and also for pre-processor directives, and .cpp files for definitions. For example, test.h might be look like

#define CONSTANT 123 // pre-processor directive
void myfunction(char* str);

and your test.cpp might look like

#include <stdio.h>
#include "test.h"

int main(int argc char **argv)
{
myfunction("Hello World");
return 0;
}

void myfunction (char* str)
{
printf("%s and constant %d", str, CONSTANT);
return;
}

Where to put include statements, header or source?

Only put includes in a header if the header itself needs them.

Examples:

  • Your function returns type size_t. Then #include <stddef.h> in the header file.
  • Your function uses strlen. Then #include <string.h> in the source file.

Where should standard library include be writen ? .c or .h file?

Header files within your application should only include system headers which are required to declare further interfaces within the header.

For example -- if your header includes functions which take a FILE * as a parameter, it should #include <stdio.h>. If it declares a structure containing a uint32_t, it should #include <stdint.h>. And so on.

System headers which are only used within the implementation should be left to the .c file. Your header should not #include <stdio.h> simply because the implementation calls printf(), for example.

Should a .c file include something that it's .h file already includes?

The second paragraph is talking about one special case: the .cc file that implements the functions declared in the corresponding .h file. Since the .cc and .h file are intended to be closely related and maintained in tandem (often by the same programmer), the .cc file can depend on what's in its related header file.

The first paragraph is talking about other files that include the header file.

So foo.cc can depend on the includes in foo.h, but bar.cc should include both foo.h and baz.h.



Related Topics



Leave a reply



Submit