Skip to content
Related Articles

Related Articles

Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 27 Oct, 2022
Improve Article
Save Article

Namespace in C++ | Set 1 (Introduction) Namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)Different ways to access namespaceIn C++, there are two ways of accessing namespace variables and functions.

Defining a Namespace:

A namespace definition begins with the keyword namespace followed by the namespace name as follows:

namespace  namespace_name 
{
// code declarations i.e. variable  (int a;)
method (void add();)
classes ( class student{};)
}

It is to be noted that, there is no semicolon (;) after the closing brace.
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows:
namespace_name: :code;  // code could be variable , function or class.

The using directive:

You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. 
The namespace is thus implied for the following code:

C++




#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;
}


Names introduced in a using directive obey normal scope rules. The name is visible from the point of the using directive to the end of the scope in which the directive is found. Entities with the same name defined in an outer scope are hidden.

Nested Namespaces:

Namespaces can be nested where you can define one namespace inside another name space as follows:

SYNTAX:
namespace namespace_name1 
{
  // code declarations
  namespace namespace_name2 
  {
     // code declarations
  }
}

You can access members of nested namespace by using resolution operators as follows:
// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;
// to access members of namespace:name1
using namespace namespace_name1;

In the above statements if you are using namespace_name1, then it will make elements of namespace_name2 available in the scope as follows:

C++




#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::second_space;
int main ()
{
    // This calls function from second name space.
      func();
   
      return 0;
}


  1. Normal way 

CPP




// C++ program to demonstrate accessing of variables
// in normal way, i.e., using "::"
#include <iostream>
using namespace std;
 
namespace geek
{
    int rel = 300;
}
 
int main()
{
    // variable ‘rel’ accessed
    // using scope resolution operator
    cout << geek::rel << "\n";  // prints 300
 
    return 0;
}


  1. Output :
300
  1. “using” directive 

CPP




// C++ program to demonstrate accessing of variables
// in normal way, i.e., using "using" directive
#include <iostream>
using namespace std;
 
namespace geek
{
    int rel = 300;
}
 
// use of ‘using’ directive
using namespace geek;
 
int main()
{
   // variable ‘rel’ accessed
   // without using scope resolution variable
   cout << rel << "\n";        //prints 300
   
   return 0;
}


  1. Output:
300

Using namespace in header filesWe can create namespace in one file and access contents using another program. This is done in the following manner.

  • We need to create two files. One containing the namespace and all the data members and member functions we want to use later.
  • And the other program can directly call the first program to use all the data members and member functions in it.

File 1 

CPP




// file1.h
namespace foo
{
    int value()
    {
       return 5;   
    }
}


File 2 

CPP




// file2.cpp - Not to be executed online
#include <iostream>
#include “file1.h” // Including file1
using namespace std;
 
int main ()
{
    cout << foo::value();
    return 0;
}


Here we can see that the namespace is created in file1.h and the value() of that namespace is getting called in file2.cpp.Nested NamespacesIn C++, namespaces can also be nested i.e., one namespace inside another. The resolution of namespace variables is hierarchical. 

CPP




// C++ program to demonstrate nesting of namespaces
#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 AliasingIn C++, you can use an alias name for your namespace name, for ease of use. Existing namespaces can be aliased with new names, with the following syntax:

namespace new_name = current_name;

CPP




#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

This article is contributed by Abhinav Tiwari .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!