Open In App

namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

We have introduced namespaces in below set 1.
Namespace in C++ | Set 1 (Introduction)

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


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 
     

CPP




// A C++ program to show more than one namespaces
// with different names.
#include <iostream>
using namespace std;
 
// first name space
namespace first
{
   int func() {  return 5; }
}
 
// second name space
namespace second
{
   int func() { return 10; }
}
 
int main()
{
   // member function of namespace
   // accessed using scope resolution operator
   cout << first::func() <<"\n";       
   cout << second::func() <<"\n";
   return 0;
}


  • 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. 
     

CPP




// C++ program to demonstrate namespace extension
#include <iostream>
using namespace std;
 
// first name space
namespace first
{
   int val1 = 500; 
}
 
// rest part of the first namespace
namespace  first
{
   int val2 = 501; 
}
 
int main()
{
   cout << first::val1 <<"\n";       
   cout << first::val2 <<"\n";
   return 0;
}


  • 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.

 

CPP




// C++ program to demonstrate working of unnamed
// namespaces
#include <iostream>
using namespace std;
 
// unnamed namespace declaration
namespace
{
   int rel = 300;
}
 
int main()
{
   cout << rel << "\n"; // prints 300
   return 0;
}


Output: 
 

300

 



Last Updated : 28 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads