Consider following C++ program.
| // A program to demonstrate need of namespaceintmain(){    intvalue;    value = 0;    doublevalue; // Error here    value = 0.0;}Output : | 
Compiler Error: 'value' has a previous declaration as 'int value'
In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name.
| // Here we can see that more than one variables // are being used without reporting any error.// That is because they are declared in the // different namespaces and scopes.#include <iostream>usingnamespacestd;// Variable created inside namespacenamespacefirst{    intval = 500;}// Global variableintval = 100;intmain(){    // Local variable    intval = 200;    // These variables can be accessed from    // outside the namespace using the scope    // operator ::    cout << first::val << '\n';     return0;}Output: | 
500
Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.
- Namespace is a feature added in C++ and not present in C.
- A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.
- Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
namespace namespace_name 
{
   int x, y; // code declarations where 
             // x and y are declared in 
             // namespace_name's scope
}
- Namespace declarations appear only at global scope.
- Namespace declarations can be nested within another namespace.
- Namespace declarations don’t have access specifiers. (Public or private)
- No need to give semicolon after the closing brace of definition of namespace.
- We can split the definition of namespace over several units.
| // Creating namespaces#include <iostream>usingnamespacestd;namespacens1{    intvalue()    { return5; }}namespacens2 {    constdoublex = 100;    doublevalue() {  return2*x; }}intmain(){    // Access value function within ns1    cout << ns1::value() << '\n';     // Access value function within ns2    cout << ns2::value() << '\n';     // Access variable x directly    cout << ns2::x << '\n';           return0;}Output: | 
5 200 100
Following is a simple way to create classes in a name space
| // A C++ program to demonstrate use of class// in a namespace#include <iostream>usingnamespacestd;namespacens{    // A Class in a namespace    classgeek    {    public:        voiddisplay()        {            cout << "ns::geek::display()\n";        }    };}intmain(){    // Creating Object of student Class    ns::geek obj;    obj.display();    return0;}Output: | 
ns::geek::display()
Class can also be declared inside namespace and defined outside namespace using the following syntax
| // A C++ program to demonstrate use of class// in a namespace#include <iostream>usingnamespacestd;namespacens{    // Only declaring class here    classgeek;}// Defining class outsideclassns::geek{public:    voiddisplay()    {        cout << "ns::geek::display()\n";    }};intmain(){    //Creating Object of student Class    ns::geek obj;    obj.display();    return0;}Output: | 
ns::geek::display()
We can define methods also outside the namespace. Following is an example code.
| // A C++ code to demonstrate that we can define // methods outside namespace.#include <iostream>usingnamespacestd;// Creating a namespacenamespacens{    voiddisplay();    classgeek    {    public:       voiddisplay();    };}// Defining methods of namespacevoidns::geek::display(){    cout << "ns::geek::display()\n";}voidns::display(){    cout << "ns::display()\n";}// Driver codeintmain(){    ns::geek obj;    ns::display();    obj.display();    return0;}Output: | 
ns::display() ns::geek::display()
Extending namespace and Unnamed namespace
It is also possible to create more than one namespaces in the global space. This can be done in two ways.
- namespaces having different names// A C++ program to show more than one namespaces// with different names.#include <iostream>usingnamespacestd;// first name spacenamespacefirst{intfunc() {return5; }}// second name spacenamespacesecond{intfunc() {return10; }}intmain(){// member function of namespace// accessed using scope resolution operatorcout << first::func() <<"\n";cout << second::func() <<"\n";return0;}Output:5 10 
- Extending namespaces (Using same name twice)
 It is also possible to create two namespace blocks having the same name. The second namespace block is nothing but actually the continuation of the first namespace. In simpler words, we can say that both the namespaces are not different but actually the same, which are being defined in parts.// C++ program to demonstrate namespace exntension#include <iostream>usingnamespacestd;// first name spacenamespacefirst{intval1 = 500;}// rest part of the first namespacenamespacefirst{intval2 = 501;}intmain(){cout << first::val1 <<"\n";cout << first::val2 <<"\n";return0;}Output:500 501 
Unnamed Namespaces
- They are directly usable in the same program and are used for declaring unique identifiers.
- In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace.
- The name of the namespace is uniquely generated by the compiler.
- The unnamed namespaces you have created will only be accessible within the file you created it in.
- Unnamed namespaces are the replacement for the static declaration of variables.
| // C++ program to demonstrate working of unnamed // namespaces#include <iostream>usingnamespacestd;// unnamed namespace declarationnamespace{   intrel = 300; }intmain(){   cout << rel << "\n"; // prints 300   return0;}Output: | 
300
 
