Originally Posted by
studentc
is namespace kind of a library or does it just describe/define some scope? i tried to do a bit of reading and i think it describes scope. does this mean that once using namespace std is mentioned, functions in the stanadrd library are global thtoroughout the program or are they just visible in the file which contains using namespace std?
Yes, it basically defines a scope that the objects are contained within. To access objects within a namespace, you can do one of three things:
#1 - put a "using namespace" declaration in your code
Code:
namespace foo
{
int x;
double d;
}
using namespace foo;
int main()
{
x = 5; // Assign the value 5 to foo::x
d = 10.0; // Assign the value 10.0 to foo::x
return 0;
}
#2 - Identify the individual member of the namespace you want to have access to with individual "using" declarations.
Code:
namespace foo
{
int x;
double d;
}
using foo::x;
int main()
{
x = 5; // Assign the value 5 to foo::x
foo::d = 10.0; // Still need foo:: in front of d because
// we did not say we were using foo::d like we did with foo::x
return 0;
}
#3 - Explicitly qualify the namespace everywhere you use it
Code:
namespace foo
{
int x;
double d;
}
int main()
{
foo::x = 5; // Assign the value 5 to foo::x
foo::d = 10.0; // Assign the value 10.0 to foo::d
return 0;
}
Originally Posted by
studentc
by the std namespace we mean functionsin the stdlib.h, correct, but isn't the cout and cin functions of the iosteam.h library, i nthat case whould it not have made only functions in stdlib.h visible
You're a little confused. The headers typically contain only function prototypes and #define statements predominately. The actual code for these functions exist in what are called libraries that are "linked" after compilation to produce the final executable program. So, iostream is not a library but rather simply a header. These headers are mostly used by the compiler to perform the task of type-checking, validating that the correct number of arguments have been passed and so on.
Headers with the old style ".h" extension in them are the older version of the newer headers (the ones without the .h extension). If you use the old headers, you will not have to do anything with regards to namespaces for the functions that are declared within them. The newer headers declare their contents within the std namespace.
In addition, the newer version of the standard C headers also have a "c" prepended to them... as an example:
Code:
Old header New header
<iostream.h> <iostream>
<stdlib.h> <cstdlib>
<stdio.h> <cstdio>
<string.h> <cstring> (not to be confused with <string> which is different entirely)
Originally Posted by
studentc
in that case by including <iostream.h> should the program not work?
As mentioned, iostream.h is the old version of the header and the functions/objects defined in it aren't in any namespace, thus you could do this:
Code:
#include <iostream.h>
int main()
{
cout << "Hello world!" << endl;
return 0;
}
...without worrying about namespaces.
However if you were to use the newer header, you would need to worry about the namespace issue in one of the three previously defined ways:
Code:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
The purpose of namespaces are to avoid conficts with other objects that could potentially be named the same thing as something you're attempting to build or use (a third party library for example). This is really more of a concern with larger projects.
[edit]Some of this might duplicate some of what grumpy said.[/edit]