Multiple .cpp File Programs

I'd like to put some of the functions in my program into another .cpp file for organizational purposes.

So, my questions are:

1. Should I put the functions into another header file or .cpp file?

2. What are the advantages to using a header file as opposed to a .cpp file? (or vise versa)

3. What's the industry standard in regards to what should be in a header file and what goes in a source code (.cpp) file?

4. If I should put it in a 2nd .cpp file, how would I include it like I do headers? (example, #include "Header.h" -- how would I do this for .cpp files?)
Last edited on
1) Your functions should go in an additional .cpp file. You want to put the declarations for those functions in a .h file which is included from your other .cpp file.

2) You use a header file for declarations so other modules know how to call your functions in the new .cpp file. This alows you to hide the implementation of those functions by only making the declarations visible in other modules.

3) Declarations go in the .h file. Implementations go in the .cpp file.

4) You don't include one .cpp file inside another .cpp file. You compile both .cpp files and let your linker combine the two resulting object files.
Thanks for the response.

4) You don't include one .cpp file inside another .cpp file. You compile both .cpp files and let your linker combine the two resulting object files.


And how do I do that? (using MS C++ 2010 Express)
Last edited on
If you include both .cpp files in your project, Visual Studio will compile both .cpp files and link the resulting .o files automatically.
I have one .cpp file which compiled fine, then I created a new .cpp file in the Source Folder (on the left, in the Solution Explorer window). I cut and pasted one of my abilities function, and there's errors everywhere. How do I link these files together so the compiler treats them as one? It doesn't seem to be doing it for me.
Lets say you have main.c, functions.h and functions.c
main.c
1
2
3
4
5
6
7
include "functions.h" 
//include "functions.c" error

int main(){
 ......
return 0;
}


functions.c
1
2
3
include "functions.h"
//functions definitions 
........


functions.h
1
2
#include<iostream>
//function declaration 
Last edited on
Lets say you have main.c, functions.h and functions.c
main.c


Thanks for the response - but I know how to include header files, my question is more specific to .cpp files.

Edit: Do I need to just include the headers on the second .cpp file instead of just including the headers on the first .cpp file?
Last edited on
except main source file, you must have corresponding .h file for every .cpp file
except main source file, you must have corresponding .h file for every .cpp file


Yeah, did that, and I moved all of my class object declarations to my main.cpp, but my second.cpp can't see those objects, and is therefore giving me errors. (error C2065: 'objectName' : undeclared identifier). How would I create a class object in a way that its scope isn't limited to the first .cpp file?
except main source file, you must have corresponding .h file for every .cpp file
Note that this is a convention. The language doesn't force you to do it this way.

Global objects are often not recommended but if you really want to have them you should define the object in a source file and use an extern declaration in the header.

1
2
// Example.h
extern int globalVar;


1
2
// Example.cpp
int globalVar /* = initialValue */;

Last edited on
Global objects are not really recommended but if you really want to have them you should define the object in a source file and use an extern declaration in the header.


Thank you, that worked. :)

Edit: I realize I shouldn't use global objects, but in this case, it makes life a bit easier. (Just don't get the wrong impression that all of my objects are global - because they aren't, just some objects for particular classes).
Last edited on
Topic archived. No new replies allowed.