As coding projects increase in size, it helps to organize logic, functions, and classes into separate files.
The example below shows how organize code across different files in a project that would be composed of the following files:
- main.cpp is the file that contains the
int main() {}
function. - factorial.cpp contains a supporting function
factorial()
called by main.cpp. - print.cpp also contains a supporting function
print()
called by main.cpp.
// #include <bits/stdc++.h>
#include <iostream>
#include<string>
// forward declaration
int factorial(int);
void print(std::string);
using namespace std;
int main() {
int num = 7;
int numFactorial = factorial(num);
// print result
string stringToPrint = "Factorial of [" + to_string(num) + "] is: " + to_string(numFactorial);
print(stringToPrint);
}
The file above calculates the factorial and constructs a string to be passed for printing. The file contains a forward declaration of the functions factorial()
and print()
, however these functions are in seperate files factorial.cpp and print.cpp.
#include <iostream>
using namespace std;
// Recursive factorial program
int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
The function above calculates the factorial of a number recursively. The factorial of a number is computed as n! = 1 * 2 * 3 * ... * n
.
#include <iostream>
#include<string>
using namespace std;
void print(string str) {
cout << str << "\n";
}
The function above prints a string to the standard output.
If you click Run, main.cpp should successfully execute with the following output.
// Factorial of [7] is: 5040
We've previously used functions such as cin, cout, ifstream, ofstream, .lenght() that have no forward declaration, but are contained in header files such as <iostream>, <fstream>, and <string>. We can also create our own header file, which we demonstrate in the example below.
- function.h is a header file that declares the supporting functions
factorial()
andprint()
.
#ifndef FUNCTION_H
#define FUNCTION_H
int factorial(int);
void print(std::string);
#endif
The file above is a header file that contains the following elements:
#ifndef ... #endif
wrapper that executes what is inclosed if the header function hasn't already been defined. This is to avoid compiling the contents of the file more than once.#define FUNCTION_H
is a macro that declares the contents of the global variableFUNCTION_H
. During compilation, all declarations of#include "function.h"
will be replaced with the function declarations contained within.int factorial(int); void print(std::string);
are forward declarations of the functions in factorial.cpp and print.cpp.
We will also need to make a slight update to main.cpp by inserting #include "function.h"
(and remove the forward declarations of functions).
// main.cpp
#include <iostream>
#include<string>
// custom header file
#include "function.h"
using namespace std;
int main() {
int num = 7;
int numFactorial = factorial(num);
// print result
string stringToPrint = "Factorial of [" + to_string(num) + "] is: " + to_string(numFactorial);
print(stringToPrint);
}
If you click Run, main.cpp should successfully execute with the following output.
// Factorial of [7] is: 5040
An advantage of the header file approach, is that we can add forward declarations to the header file (i.e. in a centralized location) rather than update all files to include the forward declarations (of which could be many lines long).