Quote: "Does including the same file multiple times waste memory and copy-pastes the code that many times?"
No, in C++ it doesn't waste anything, because by including your .h header files, you just pass in the declaration of your functions (you can think of a memory address where the actual function code is stored just once). The code of the function is usually implemented in some .cpp file.
There may be a lot more to explain about it, but a more easy explanation is this: With you're header files you just tell all your other files (that include this header) the name of the functions (and other stuff), the actual code that's behind these function names is then written in some other code file.
Here is an example:
->
MyMathFunctions.h
// Make sure the file is just compiled once
#pragma once
// Function declarations
int add( int a, int b );
int multiply( int a, int b );
->
MyMathFunctions.cpp
// Include the header file, to tell us the function names
#include "MyMathFunctions.h"
// The add function code
int add( int a, int b )
{
int result = a + b;
return result;
}
// The multiply function code
int multiply( int a, int b )
{
int result = a * b;
return result;
}
You can then just include the header file in your program files to use these functions. The compiler will then look up the functions code in the code file.
Note: You could also split the code implementation into multiple files which is handy for larger functions or classes. As an example here are the functions from the same header as above split into multiple files:
->
MyMathFunctionsAdd.cpp
// Include the header file, to tell us the function names
#include "MyMathFunctions.h"
// The add function code
int add( int a, int b )
{
int result = a + b;
return result;
}
->
MyMathFunctionsMultiply.cpp
// Include the header file, to tell us the function names
#include "MyMathFunctions.h"
// The multiply function code
int multiply( int a, int b )
{
int result = a * b;
return result;
}
I hope this makes some sense and that it helps you a bit.