why should i include the header file <iostream> after using the namespace std?

When you do #include <iostream> it causes a set of classes and other things to be included in your source file. For iostream, and most of the standard library headers, they place these things in a namespace named std.

So the code for #include <iostream> looks something like this:

namespace std { 
    class cin  { ... };
    class cout { ... };
    class cerr { ... };
    class clog { ... };
    ...
}

So at this point, you could write a program that looks like:

#include <iostream>

int main() {
    std::cout << "hello\n";
    return 0;
}

Now, some people feel that std::cout is too verbose. So they do:

#include <iostream>
using namespace std;

int main() {
    cout << "hello\n";
    return 0;
}

Personally, I'd recommend against this, and if you really feel that std::cout is too verbose, then I'd suggest that you use a smaller using statement.

#include <iostream>
using std::cout;

int main() {
    cout << "hello\n";
    return 0;
}

If you're wondering why I would recommend against using namespace std, then I would forward you to the following two other posts on stackoverflow:

  • C++ Distance Function Keeps Returning -1
  • Why is "using namespace std" considered bad practice?

The compiler itself does not have the definitions of the things that are in any namespace (whether it is std or some other namespace). That is the role of source files and header files.

What using namespace std; tells the compiler is that "If you can't find some name in the current namespace, go look in the std namespace as well".

What #include <iostream> tells the compiler is that you want the contents of the header called iostream to be included in your sources. This will provide the compiler with code to do cin, cout and a lot of other related functionality. The content of this file is declared like namespace std { ... all the stuff goes here ... }.

The usage of namespace allows someone else, working in namespace math; to not have to worry about "Hmm, what do I do now, I need a counter for number of entrances, let's call it cin - but hang on, is that ever used anywhere?".

This may not be the greatest of examples, but in large projects, it gets increasingly hard to keep track of things and what names they have. And C++ is a language intended for large projects of millions of lines of code - and now it starts getting hard to remember if you've used a particular name or not. Namespaces make sure that you don't have to worry about it outside of a particular namespace.

(Oh, and in my code, I tend to not use using namespace std;, but write std::cout << "Hello, World!" << std::endl; - this helps to make it clear that the cout I'm using here is the std one, and not something else. This is particularly useful when you have several namespaces with similar things, like in my own compiler, where I have my compiler with it's functionality, the std namespace providing some things, and llvm compiler things - if I were to stick using namespace llvm; at the beginning of the code, it would be very hard to track whether Type* p = ...; is from LLVM or some part of my own code.)