External Resources

C++ Crash Course

Major differences between Python or MATLAB and C++

Using Header (.h) and C++ files (.cpp)

Header (.h) files are commonly used alongside files with C++ code (.cpp) files. A header file includes information that would go at the top of your program if the program was in main.cpp. They are useful for keeping code organized.

What goes in a header file vs. C++ is a matter of convention, not a strict requirement. By convention:

Note that the header / C++ file pairing only applies for C++ files that are outside of main.cpp; if everything is in one file, you can put whatever would be in a header file at the top of the file (hence the name “header”). Header files also contain special directives to ensure that the declarations in the file are only included once per project (see below). The underscores around the header name are also a convention and not required.

Here’s an example header and C++ setup:

Example header file (add.h)