Let's say you have your "main" CPP file, which will have your main()
function, a CPP file that does some stuff, and a header file that declares what that stuff is:
main.cpp:
#include "stuff.h"
#include
int main() {
cout << do_something(5) << endl;
}
stuff.h:
int do_something(int);
stuff.cpp:
#include "stuff.h"
int do_something(int x) {
return x*x;
}
If you are using an IDE like QtCreator or Visual Studio, you shouldn't have to do anything special.
If you use the command line, you now need to let it know where these extra files are. For example, using GCC:
g++ main.cpp stuff.cpp -o the_program_name
(Assuming your header file is in the same directory as the CPP files)
Now, the program will write to standard output 25
, because it is calling the do_something
function from stuff.cpp