WHY do you split files?

Started by
3 comments, last by Ace826 19 years, 6 months ago
I understand that in many larger programs you would have to split the source into different source files (.cpp, .h). But what do you put in each. Do you only put specific data in .h's or .cpp's and what is the realtionship between two .cpp's in the same file? Thanks
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Advertisement
Organization and maintainability is the primary reason.
A header, and its corresponding code files would have related items of code in them. For instance, a header might declare an object, but the .cpp would actually define the methods. This way when you need to change the object, you don't go digging through one huge file, but instead a couple of small headers and source files.

It also means that when compiling, and linking, you can use partial rebuilds. Thus reducing the amount of time spent compiling and linking the application due to changes.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

http://www.gamedev.net/reference/articles/article1228.asp

-me
Source code is split into many different files for many reasons. As a simple example, you could have graphics.h and .cpp, audio.h and .cpp and game.h and .cpp. Then later you could reuse audio and graphics and write a new game without having to create new engines.

Quote: what is the realtionship between two .cpp's in the same file?


I'm not really sure what you mean by this because two cpp's will be in two different files...
Lucas Henekswww.ionforge.com
I usually put function declarations and class declarations. In the header files remember to add the #ifndef, #define, and #endif. So it doesn't get included more then once. In the .cpp files i put the actual funcion code. Then it will all get linked together. If you want to use the functions just delcare the .h file. It is really hard to get used to how everything is linked together, but in the end, it is very kool.

If you have any problems, PM me. I have manage with help from this forum, and other resources on the net, to figure most of it all out.

This topic is closed to new replies.

Advertisement