Namespaces in C++

Namespaces in C++: The namespace in C++ can be used to overcome the difficulty of using the same name for many times. And is used as additional information to differentiate similar functions, classes, variables, etc. With the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.

Namespaces in C++

Defining the Namespace

A namespace definition begins with the keyword namespace. For accessing the class of a namespace, we need to use namespace name:: class name. We can use using keywords so that we don’t have to use the complete name all the time.  In C++, global namespace is the root namespace. The global:: std will always refer to the namespace “std” of C++ Framework.

Syntax:

namespace namespace_name {
// code declarations
}

Rules for Declaring the Namespaces in C++

  • declarations appear only at global scope.
  • declarations can be nested within another namespace.
  • Its declarations don’t have access specifiers (public or private).
  • We need to give a semicolon after the closing brace of definition.

Example:

#include<iostream>  
using namespace std;  
namespace First {    
    void sayHello() {   
        cout<<"Hello First Namespace"<<endl;          
    }    
}    
namespace Second  {    
       void sayHello() {   
           cout<<"Hello Second Namespace"<<endl;   
       }    
}   
int main()  
{  
 First::sayHello();  
 Second::sayHello();  
return 0;  
}

Output:

Hello First Namespace
Hello Second Namespace

Example: by using keyword

#include<iostream>  
using namespace std;  
namespace First{  
   void sayHello(){  
      cout << "Hello First Namespace" << endl;  
   }  
}  
namespace Second{  
   void sayHello(){  
      cout << "Hello Second Namespace" << endl;  
   }  
}  
using namespace First;  
int main () {  
   sayHello();  
   return 0;  
}

Output:

Hello First Namespace

Using Directive

The directive tells the compiler that the subsequent code is making use of names in the specified namespace.

Example:

#include<iostream>
using namespace std;
//first name space
namespace first_space {
   void func() {
      cout<<"Inside first_space" << endl;
   }
}
//second name space
namespace second_space {
   void func() {
      cout<<"Inside second_space" << endl;
   }
}

using namespace first_space;
int main () {
//This calls function from first name space.
   func();
   
   return 0;
}

Output:

Inside first_space

The ‘using’ directive can also be used to refer to a particular item within a namespace.

Discontiguous Namespaces

A namespace is made up of the sum of its separately defined parts. The separate parts of a namespace can be spread over multiple files. So, if one part of the namespace requires a name defined in another file, that name must still be declared.

Syntax: 

namespace namespace_name {
// code declarations
}

Nested Namespaces

Namespaces can also be nested i.e. one namespace inside another. The resolution of namespace variables is hierarchical.

Example:

#include <iostream> 
using namespace std; 
//Nested namespace 
namespace out 
{ 
  int val = 5;  
  namespace in 
  { 
      int val2 = val;     
  } 
} 
//Driver code 
int main() 
{ 
  cout << out::in::val2;//prints 5 
  return 0; 
}

Output:

5

Namespace Aliasing

We can use an alias name for your namespace name, for ease of use. Existing namespaces can be aliased with new names.

Syntax: namespace new_name = current_name;

Example:

#include<iostream> 
namespace name1  
{ 
    namespace name2  
    { 
         namespace name3  
         { 
             int var = 42; 
         } 
    } 
}  
//Aliasing  
namespace alias = name1::name2::name3;  
int main() 
{ 
    std::cout << alias::var << '\n'; 
}

Output:

42