- #1
JonnyG
- 234
- 44
Let's take the cout function for example. That's included in the iostream library (is library the correct word here?) which is why I type #include <iostream> at the beginning of my program. Now when I want to use the cout function, I either have to type std::cout or I can just type cout but have to write using namespace std or using std::cout before my main() function.
I was told that the reason why we have namespaces is because what if we use another library that also has a cout function for example? The compiler won't know which cout function to use. But why do we associate cout with std? Isn't cout a function in the iostream library? So wouldn't it make more sense to type iostream.cout or iostream::cout when we want to use the cout function that specifically belongs to the iostream library?
What is the purpose of the namespace in this sense and what is the relationship between the namespace std and the library iostream?
I was told that the reason why we have namespaces is because what if we use another library that also has a cout function for example? The compiler won't know which cout function to use. But why do we associate cout with std? Isn't cout a function in the iostream library? So wouldn't it make more sense to type iostream.cout or iostream::cout when we want to use the cout function that specifically belongs to the iostream library?
What is the purpose of the namespace in this sense and what is the relationship between the namespace std and the library iostream?