In C++ programming if we didn’t include “using namespace std;” we would need to reference cout, cin and other functionality with the namespace for each use by typing “std::” in front. For example if we didn’t include “using namespace std;” and wanted to use cout we would then need to type std::cout so that cout could be located and found. For a real life example think of the use of “std” as a location or address on a Google map.
The include statements give us access to the code for the standard library. The declarations for the Standard C++ library are inside these files we include (we include iostream to get the cout code). The include statement includes the C++ language code inside the file as part of our program. For a real life example think of including a code/header file as the Google Map itself. In order to travel anywhere we need the Map (the include) and the location we’re traveling (the “std” namespace).
The files we include using the include statement are code files included in the C++ language and their location or path is operating system (OS) dependent and different. We load and pull them in when we include them.
You would not be able to declare variables of type string if you did not include the code/header file and the namespace std. We need to include the code in our program to work with strings which we do through the include statement and we need to reference its location which we do via the std namespace. In our map example we would not be able to travel to a destination without the map and the address. Think of the “include” statement as including our Map and the “using” statement or “std::” prefix as providing a location.
It’s the way the language was designed and has developed. C++ has evolved and developed over the years and the engineers of the language added and designed the functionality for strings in this way. The standard string type was not always part of the language before 1998. I’m humbled by what they’ve achieved and I can appreciate the complexity and difficulties of writing and support a language and its development.
Content Excerpt from the Udemy course “How to program in C++ from Beginner to Paid Professional” (discount applied if your interested in learning more and taking this complete C++ course).