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.
iostream
is the name of the file where cout is defined. On the other hand,std
is a namespace, equivalent (in some sense) to java's package.cout is an instance defined in the
iostream
file, inside the std namespace.There could exist another
cout
instance, in another namespace. So to indicate that you want to use thecout
instance from thestd
namespace, you should writestd::cout
, indicating the scope.To avoid the
std::
everywhere, you can use theusing
clause.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
cout
exist, in different namespacesAfter including
<iostream>
, you'd still need to specify the namespace. The#include
statement doesnt say "Hey, use the cout in std::". Thats whatusing
is for, to specify the scopeIf your C++ implementation uses C style header files (many do) then there is a file that contains something similar to:
std is the namespace that the C++ standard says most of the standard things should reside in. This is to keep from overpopulating the global namespace, which could cause you difficulty in coming up with names for your own classes, variables, and functions which aren't already used as names for standard things.
By saying
you are telling the compiler that you want it to search in the namespace std in addition to the global namespace when looking up names. If the compiler sees the source line:
somewhere after the
using namespace std;
line it will look forfoo
in various different namespaces (similar to scopes) until it finds a foo that meets the requirements of that line. It searches namespaces in a certain order. First it looks in the local scope (which is really an unnamed namespace), then the next most local scope until over and over until outside of a function, then at the enclosing object's named things (methods, in this case), and then at global names (functions, in this case unless you've been silly and overloaded () which I'm ignoring), and then at the std namespace if you've used theusing namespace std;
line. I may have the last two in the wrong order (std may be searched before global), but you should avoid writing code that depends on that.cout
is logically defined within iostream. By logically, I mean it may actually be in the file iostream or it may be in some file included from iostream. Either way, including iostream is the correct way to get the definition ofcout
.All symbols in iostream are in the namespace
std
. To make use the thecout
symbol, you must tell the compiler how to find it (i.e. what namespace). You have a couple of choices:The
#include <iostream>
references the header file that defines cout. If you're going to use cout, then you will always need the include.You do not need to
using namespace std;
. That's simply allows you to use the shorthandcout
andendl
and the like, rather thanstd::cout
andstd::endl
where the namespace is explicit. Personally I prefer not to useusing namespace ...
since it requires me to be explicit in my meaning, though it is admittedly more verbose.