In: Computer Science
how do i run 3 files on c++?
The solution is as follows:
Initially, for the small programs there is no need to implement the code in multiple code files but, when the programs get larger interms of code lines and other perspectives it is common to split them into multiple files for organizational or reusability purposes. And these needs to be implemented in proper IDE is that they make working with multiple files much easier.
Always a program contains a main file which later connects to multiple sub files. It can be illustrated better with the following example.
Consider the addition.cpp file:
#include <iostream>
int main()
{
std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n';
return 0;
}
int add(int x, int y)
{
return x + y;
}
In the above code file the program is written to implement the add function for any two given values.
And splitting them involves add.cpp and main.cpp
where in main.cpp the the input is taken and the add function is splitted into another file as it is just a function. when we deal with multiple ffunctions this process will be helpful.
add.cpp
int add(int x, int y)
{
return x + y;
}
main.cpp
#include <iostream>
int add(int x, int y); // needed so main.cpp knows that add() is a function declared elsewhere
int main()
{
std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n';
return 0;
}
And when we an IDE simply create a main file such as main.cpp and later start adding the c++files if exists or create a new one and later add to the project
Some of the IDE ecamples are: VSCode, Visual Studio, Spider etc,.