It can be inconvenient to write a long program within a single file. When programs grow to hundreds or thousands of lines, breaking them into modular units helps manage the program.This way you can bring modularity in your project.Both C & C++ allows you to break your program in multiple source files.This article will discuss some of those techniques.
Header files
You can write the most commonly used functions and variables in header file. Then call the header file in your source file. As C++ knows where to look for function declaration as at the top, the call for header file is mentioned. Itll ensure that call and the definition both are consistent.
To use the header file in source file, all you have to do is add the #includefilename. Some example situation to use this is creating function declaration for all the functions you are using throughout your program & add it in header file. Then #include the file in every source file which uses function declaration.
What is #include ?
It is preprocessor directive that instructs the compiler to include the contents of file in the source file.There are two ways we can use it our program.
#include "file"
#include <file>
In first statement the filename is mentioned between quotes.This statement causes compiler to look for the header in same directory as that of source file,if it’s not found then it’ll search other include directories.In second statement,filename is mentioned in brackets.This causes compiler to search the header file in directory where compiler is configured to look for or in directory assigned in project options of IDE’s.
Apart from header files you can use another .cpp or .c file to add to your source file if youve used any declaration from that file. Just #include that file at the top of the source file.
IDE >Project
Most of the modern IDEs have the solution to this problem as they use most IDE may contain the source file or even resource files. When you create new project in the IDEs it allows you add all source files to it, so that it knows which files belong together.IDE’s like Dev-C++ & Visual Studio have this option which allows you to manage multiple source files.
I hope information above helped. If you’ve any suggestions or feedback regarding this article then please do not hesitate to submit.