Why do we need both using namespace and include directives in C++ programs ?
For example,
#include <iostream>
using namespace std;
int main() {
cout << "Hello world";
}
Why is it not enough to just have #include or just have "using namespace std" and get rid of the other ?
(I am think of an analogy with Java, import java.net.* will import import everything from java.net, you don't need to do anything else.)
In C++, the include directive will copy and paste the header file into your source code in the preprocessing step. It should be noted that a header file generally contains functions and classes declared within a namespace. For example, the
<vector>
header might look similar to something like this:Supposing you need to define a vector in your main function, you do
#include <vector>
and you have the piece of code above in your code now:Notice that in your code the vector class is still located in the
std
namespace. However, your main function is in the defaultglobal
namespace, so simply including the header will not make the vector class visible inglobal
namespace. You have to either useusing
or do prefixing likestd::vector
.The include is defining the existence of the functions.
The using is making it easier to use them.
cout
as defined in iostream is actually named "std::cout".You could avoid using the namespace by writing.
I think the other answers are missing the point slightly. In all of C++, Java and C#, the
using
/import
thing is entirely optional. So that's not different.And then you have to do something else to make code be visible anyway, in all three platforms.
In C++, you minimally have to include it into the current translation unit (good enough for many implementations of vector, string, etc.), often you have to add something to your linker as well, although some libraries do that automatically based on the include (e.g. boost when building on Windows).
And in C# you have to add a reference to the other assembly. That takes care of the equivalent of includes and link settings.
And in Java you have to ensure the code is on the classpath, e.g. adding the relevant jar to it.
So there are very closely analogous things required on all three platforms, and the separation between
using
/import
(a convenience) and actual linkage resolution (a requirement) is the same in all three.You need to understand namespaces if you want to truly understand this.
With
include
you are just including the header file.With
using namespace
you are declaring you are using a given namespace that contains stuff such as cout. so if you do this:to you use
cout
you can just doinstead of:
Note that if you do not
#include <iostream>
you won't be able to use neitherstd::cout
norcout
in your declarations and so forth because you're not including the header.One liner (not that this is something new :)):
using std allows you to omit std:: prefix, but you cannot use cout at all without iostream.
These keywords are used for different purposes.
The using keyword makes a name from a namespace available for use in the current declarative region. Its mostly for convenience so that you do not have to use the fully qualified name all the time. This page explains it in some detail.
The #include statement is a pre processor directive and it tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. That is, you can think of this statement as copying the included file into the current one. The compiler then compiles the entire file as if you wrote all the code in one big file.