Namespaces organise identifiers such as variables, functions, and classes into logical groups. This allows the global namespace to be arranged into different scopes preventing any naming collisions when the codebase includes multiple libraries. Inside a namespace, identifiers declared within that namespace can be referred to directly, without any namespace qualification. To refer to objects from outside the namespace scope use the scope resolution operator preceded by the name of the namespace. It's possible to declare more than one namespace with the same name thus allowing a namespace to be split over several files, or even separated within the same file.

A namespace definition begins with the keyword namespace followed by the namespace name and the list of identifiers enclosed within curly brackets.

#include <iostream>
namespace ns //creates namespace ns 
{ 
int x=1, y=5; 
double value() 
{ 
return x+y; 
} 
} 
using namespace std; 
int main () 
{ 
cout << "x after call: " << ns::value(); //call function value in namespace ns 
cout << endl; 
return 0; 
}

The Using Directive

To avoid the perpetual use of the scope resolution operator when referring to the members of the namespace, the using directive tells the compiler that the subsequent code is using names in the specified namespace defined by the using directive.

#include <iostream>
using namespace std;
namespace first_nspace {// define 1st name space
void func()
{
cout << "first name space" << endl;
}
}
namespace second_nspace { // define 2nd name space
void func()
{
cout << "second name space" << endl;
}
}
using namespace first_nspace; //set first_nspace as default
int main ()
{
func(); //Calls func() from first and default name space.
second_nspace::func();//Calls func() from 2nd name space using scope resolution.
return 0;
}

Unnamed Namespaces

Unnamed namespaces allow the creation of unique identifiers that are known only within the scope of a single file. Members of that namespace may be used directly, without qualification however outside the file, the identifiers will not be recognised.

If a file contains the following declaration-

namespace {
int v=10;
}
void f1( ) {
cout << v; 
}

The same variable v will not be recognised outside that file-

extern int v;
void f2( ) {
cout << v; // error
}

The std Namespace

The standard C++ library is contained within a namespace called std. The directive using namespace std allows this standard library to be brought into the current namespace without having to qualify each reference with the prefix std::. If a program is only making limited use of the standard C++ library it may make more sense to use the define statement on an individual basis -

#include <iostream>
using std::cout; //allows cout to be used in current namespace
using std::cin;  //allows cin to be used in current namespace
int main()
{
double val;
cout << "Enter a number: ";
cin >> val;
cout << "This is your number: ";
cout << val;
return 0;
}

Nested Namespaces

Namespaces can be defined inside another namespace. To access members of nested namespace use the resolution operator

#include <iostream>
using namespace std;
namespace first_nspace {// first name space
void func()
{
cout << "first name space" << endl;
}
namespace nested_nspace {//nested name space
void func()
{
cout << "nested second_space" << endl;
}
}
}
//include nested scope within current scope
using namespace first_nspace;
int main ()
{
func();//call func() from with existing scope
first_nspace::nested_nspace::func();//call func() using scope resolution operator
return 0;
}