How to Create Namespace in c++ Programming Language ?

What is Namespaces in C++ Programming Language:

Using namespace we can have class,global variables,functions under one name. We can change global scope to Sub-scope.

Namespace in c++

Namespace in c++

  • Namespace is a logical compartment used to avoid naming collisions.
  • The naming conflict or Collision always Occurs may occur -
  • When same program is using more than one library with some variables or functions having same name.
  • The name collision may occur for global variables, global functions, classes etc.
  • Default namespace is global namespace and can access global data and functions by proceeding (::) operator.
  • We can create our own namespace. And anything declared within namespace has scope limited to namespace.

Creating a namespace:

  • Creation of namespace is similar to creation of class.
  • 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.

Syntax:

namespace namespace_name
{
//member declarations
}

Live Example :

//In firstHeader.h
namespace ns1
{
   class one
   {
      //----Declarations---
   };
   class two
   {
      //----Declarations---
   };
}
//In secondHeader.h
namespace ns1    // can continue the defination of ns1 over multiple header files
{
   class three
   {
      //----Declarations---
   };
}

What is Unnamed Namespace ?

  • Unnamed namespaces are the replacement for the static declaration of variables.
  • 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.
#include<iostream>
using namespace std;
namespace
{
    int i;
}
int main()
{
    i=10;
    cout<< "Value : "<<i;
    return 0;
}

Output:

Value : 10

How to Use Namespaces in C++ Programming Language :

Following are the ways to refer to namespace members:

Way 1 : Using Scope Resolution :

In this method, we use the namespace name, scope resolution operator (::) and member of that namespace.

Way 2 : Using Directive:

We can use ‘using’ directive to specify the namespace.

#include<iostream>
using namespace std;
namespace first
{
int i;
}
namespace second
{
int i;
}
int main()
{
first::i=1;   //scope resolution
second::i=2;
using first::i; //using directive
cout<<i;
}

Way 3 : Using declaration:

It is a declaration within the current scope. This means it can override names from a using directive.

#include<iostream>
using namespace std;
namespace first
{
int i;
}
using namespace first;// using declaration
int main()
{
i=2;
cout<<"Value : "<<i<<endl;
}

Output:

Value : 2

Different Verities of Using Namespace :

1.We can Have Class Definition inside Namespace

#include <iostream>
using namespace std; 
namespace MyNameSpace { 
  int num1; 
  int num2; 
  class Student
  { 
     int marks; 
   public: 
     Student() {  
        marks = num1; 
     } 
  }; 
} 
int main() 
{ 
  MyNameSpace::num1 = 70; 
  MyNameSpace::num2 = 90; 
  MyNameSpace::Student ob1(); 
}
  • num1,num2 are two variables under same namespace name “MyNamespace”.
  • We can Initialize them in main function by using Scope Resolution Operator.
  • We can Declare Class inside Namespace and thus we can have multiple classes with same name but they must be in different namespace.

2.We can Have Same Class Definition inside different Namespace

#include <iostream> 
using namespace std; 
namespace MyNameSpace1 { 
  int num1; 
  int num2; 
  class Student
  { 
     int marks; 
   public: 
     Student() {  
        marks = num1; 
     } 
  }; 
} 
namespace MyNameSpace2 { 
  int num1; 
  int num2; 
  class Student
  { 
     int marks; 
   public: 
     Student() {  
        marks = num1; 
     } 
  }; 
} 
int main() 
{ 
  MyNameSpace1::num1 = 80; 
  MyNameSpace1::num2 = 50; 
  MyNameSpace1::Student ob1(); 
}