what is the difference between "using namespace std" and #include <iostream>. I know iostream is included to use cout function. But when using dev C++ platform, you also have to include "using namespace std" to make use of iostream. Does anybody know why we also have to add the namespace. the compiler gives an error if I just include the iostream and don't include "using namespace std"
usingnamespace std instructs the compiler that when it goes looking for a function or class, it should look in the namespace called std.
#include <iostream> instructs the preprocessor to find the file named iostream and copy that file completely into that spot.
They are completely different and have nothing to do with each other.
Does anybody know why we also have to add the namespace
Because that's how C++ works. To keep things tidy and make it harder to accidentally use the same name as a function or class that already exists, we can put them into little sections that we call "namespaces", and then to use the contents of each little section we have to say that we want to use the contents.
We use namespaces in real life. If you say you are looking for John, that is only their first name - there are many people named John. You need the last name to narrow it down specifically to John Anderson or John Henson.
For example, cout is the first name, std is the last name. Just as you might write "Anderson, John" in the "last, first" notation, in C++ you write std::cout.
oh I see. cout is the object of class ostream which is part of iostream. That makes sense. But the compiler still doesn't detect cout if I don't use the namespace std;
But the compiler still doesn't detect cout if I don't use the namespace std;
Because this is how namespaces work. You have to tell the compiler that you want to use the contents of the namespace named std. If you don't tell the compiler this, it will not use the contents of the namespace named std.
So, if I tell you that cout is inside the namespace std, can you now understand why the compiler will not find cout if you don't tell it that you want to use the namespace named std?
@LB I read your post.
I came to conclusion that the object cout is inside iostream that's why we need iostream and iostream uses certain statement inside its program that are called from namespace. That's why we need namespace.
No. "iostream" is an actual text file on your computer, it just doesn't have an extension like .h .cpp .txt etc. The reason everything is in the std namespace is simply a design choice - it could have been in any namespace with any name or no namespace at all, but to make your life easier (I'm not kidding) they chose to put it in a namespace with a short name (std is short for standard).
I am confused. LB is saying that everything is in the namespace and Fredbill30 is saying that iostream defines cout. Is cout inside namespace std or the header file. :S I still think cout is inside header file but uses certain entities that are inside namespace.
The std namespace is defined inside of the iostream header file. Just because a file doesn't have an extension doesn't mean you can't read what's inside of it just tell Windows to use Word pad and you can see what I mean plain as day.