I am working on a program for my CS class. This is a simulation of the delivery company at the airport.
This is a very simple small program consisting of several header and source files and a main.cpp source file that organizes the simulation.
There are certain predetermined constant values, such as the frequency of arrival of the cargo, the carrying capacity of the aircraft, the amount of time it takes the worker to process certain elements, etc. (all these are integer values). I need to access these variables in several functions in main.cpp
It seems reasonable to declare these main () functions as const ints, effectively making them global, for example.
const int kTotalTime = 2000;
const int kPlaneCapacity = 25;
int main(){//...program code}
I know that in most situations global variables should be avoided, since there are no restrictions on where they can be called and / or changed, which can lead to accidental violations of parts of the program, which, in turn, can be difficult to debug, and compatibility issues for future code, etc. However, since they are read-only values of the primitive data type that are used throughout the program, this seemed like a reasonable solution. In addition, he makes an explicit statement about the purpose of variables for all who read the code, as well as the compiler.
Questions: Am I mistaken in my logic? How so? When is global variables (const or not) wise to use? If this is a bad decision, then how would you suggest declaring read-only constant values like these?
Thanks so much for your time!