What is the relationship between iostream and namespace std?

All of the standard library definitions are inside the namespace std. That is they are not defined at global scope, so in order to use them you need to qualify them in one of the following way:

  • std::cout
  • using namespace std
  • using std::cout

For instance lets take this:

// declarations
int global_variable;

namespace n {
int variable2;
}

global_variable can be access as it is:

int x;
x = global_variable;

But variable2 is not part of the global space, but part of the namespace n.

int x;
x = variable2; // error variable2 identifier not found.

So you have to use the fully qualified name:

int x;
x = n::variable2;

As a shortcut you can write:

using namespace n;
int x;
x = variable2; // variable2 is searched in current namespace
               // and in all namespaces brought in with using namespace
               // Found ONLY in namespace n -> OK

or

using n::variable2; // this makes any unqualified reference to `variable2`
                    // to be resolved to `n::variable2`
int x;
x = variable2;

As for the header files, iostream.h was used by many compilers before there was a standard. When the committee tried to standardize they decided to make the C++ headers extensionless in order not to break compatibility with existing code.


1.If I am including the iostream library, why is a namespace needed to find cout? Is there another cout somewhere that could cause a name clash?

It is needed because the C++ standard requires that cout be inside the std namespace. There could be a clashing cout, but not in the standard library (e.g. your own code, or some third party library.)

1.What exactly was iostream.h before it was changed to iostream?

It could be anything, because it is not part of the standard, but it was the name of a pre-standardization header which formed the basis for iostream. Usually, it declared all names in the global namespace, so it is likely that the example you are looking at was written pre-standardization.

2.Did namespace play a part in this change?

This question is unclear. The keyword namespace may be used inside implementations, and it is used to declare and define data, functions, types etc. inside a namespace. So it did have some part in this change.

namespace foo
{
  void bar();  // declares foo::bar
}

Because this line starts with #, it is called a "preprocessor directive". The preprocessor reads your program before it is compiled and only executes those lines beginning with #. The preprocessor sets up your source code for the compiler.

The #include directive causes the preprocessor to include the contents of another file into the program. The iostream file contains code that allows a C++ program to display output to the screen and take input from the keyboard. The iostream files are included in the program at the point the #include directive appears. The iostream is called a header file and appears at the top or head of the program.

using namespace std;

C++ uses namespaces to organize names or program entities. It declares that the program will be assessing entities who are part of the namespace called "std." Every name created by the iostream file is part of that namespace.