for using cout, I need to specify both:
#include<iostream>
and
using namespace std;
Where is cout defined? in iostream, correct? So, it is that iostream itself is there in namespace std?
What is the meaning of both the statements with respect to using cout?
I am confused why we need to include them both.
iostreamis the name of the file where cout is defined. On the other hand,stdis a namespace, equivalent (in some sense) to Java’s package.cout is an instance defined in the
iostreamfile, inside the std namespace.There could exist another
coutinstance, in another namespace. So to indicate that you want to use thecoutinstance from thestdnamespace, you should writestd::cout, indicating the scope.To avoid the
std::everywhere, you can use theusingclause.They are two different things. One indicates scope, the other does the actual inclusion of
cout.In response to your comment
Imagine that in iostream two instances named
coutexist, in different namespaces:After including
<iostream>, you’d still need to specify the namespace. The#includestatement doesn’t say "Hey, use the cout in std::". That’s whatusingis for, to specify the scope.