C++

C++ Namespaces

Namespace is a container for identifiers. In C++, if we want to require some declaration like some variables, functions and classes in our program, we put it into a group. This group is called Namespace. So, namespace is a group of declarations.

Identical Features of Namespace

a. Namespace is generally a keyword, and we use namespace with any unique name in our program.

b. We use namespace in our source file. Namespace can also be used in user defined header file. It means we create a header file and inside the header file, we can use namespace.

c. Namespace puts the name of its member in a distinct space, so that they don’t conflict with the names in other namespaces or global namespaces.

Creating a Namespace in C++ Program

Namespace MyExample {

// Declarations

}

a. Unlike class definition, namespace definition doesn’t terminate with a semicolon.

b. Namespace has to be declared at global scope or nested inside another namespace (1).

c. Namespace has another important feature. We can use an alias name for our namespace name. It can be used as:

namespace me = MyExample ;

Here, me is the alias name of the namespace MyExample. If we declare it once, then we can use this namespace MyExample as me throughout the program.

Unnamed Namespaces

A namespace having not any name can also be allowed in C++ program.

Namespace

{

   // declarations

}

Programming Example 1:

#include <iostream>

using namespace std ;         //using namespace;
namespace MyExample {
    int x ;
    int fun1() ;
    class Example
      {
    public :
        void fun2() ;
      };
}
int MyExample :: fun1()
   {
cout<<“ Hello fun1 :\n ” ;
   }


void MyExample :: Example :: fun2() /* :: scope resolution /operator is used here.*/
          {
        cout<<“ Hello fun2 :\n ” ;
  }
using namespace MyExample ;   //namespace is used here.
     int main()
    {
       x = 6 ;
       fun1() ;
       Example ob ;     // create an instance of Example class
    ob.fun2() ;
    }

Output:

Explanation:

Here, we declared that a namespace is called MyExample. Inside the namespace, we declared a variable ‘x‘and a function fun1(). A class Example is also declared inside the namespace. Another function called fun2() is declared in the class Example.

If we assign some values in the variable ‘x’ in our main function, we have to write

MyExample :: x = 6 ;

Because x variable is not declared inside the main function. It is the member of the namespace MyExample. So, we have to write:

Namespace :: x ;

:: is called Scope Resolution Operator. We have to write namespace:: x = 6 inside the main function to access the variable x.

If we want to call the function fun1(), we have to define it. We have to write:

int MyExample :: fun1() {

}

fun1() is the member of MyExample namespace, we have to define it. We have to define fun1() with its namespace and scope resolution operator (::).

If we want to call the fun2() inside the class Example, we have to write:

void MyExample :: Example :: fun2() ;

As the function fun2() is the member of the namespace MyExample, and inside the class Example we have to write this:

using namespace std ;

Inside the fun1() we use cout statement. It is a predefined object so, its declaration exists inside a namespace called std. This namespace is already defined inside a file named iostream. We have to include this file inside our program.

Programming Example 2:

#include<iostream>

using namespace std ;
namespace MyExample
{
   int x ;
   void fun1() ;
   class Example
     {
    public:
    void example()
      {
        Cout<<“ example is a function :\n ” ;
      }
};
}
using namespace MyExample ;
 void MyExample :: fun1()
  {
cout<<“ in fun1 ” ;
}

int main()
   {
      x = 6 ;
      Example ob ;
ob.example() ;
       fun1();
}

Output:

Explanation:

A namespace definition can be continued and extended over multiple files (1).

file1.h

namespace MyExample { int x, y ;

void fun1() ;

}

file2.h

namespace MyExample { int u, v ;

void fun2() ;

}

Suppose we have two header files called file1.h and file2.h . In file1.h, we use a namespace called MyExample. Inside the MyExample are two variables x and y and a function called fun1() is declared.

In file2.h, we use another namespace with same name called MyExample. Inside the MyExample are two variables x and y and a function called fun2() is declared.

If we include these two files in our program, we may have a misconception that there are two different namespace having the same name that exist. Name conflict may occur. Actually, only one namespace MyExample exist in the program. Within this same namespace, all variables x, y, u , v and fun1(), fun2() are add ons. Two different namespace cannot exist here.

Conclusion:

Discussing in detail about the Namespace, we have come to this conclusion that namespace provides us a unique feature in our C++ program that is on how to organize our declaration elements like variables, functions, classes in a systematic manner.

Source:

(1) studytonight.com. N.p., Web. 12 Apr. 2022. https://www.studytonight.com/cpp/namespace-in-cpp.php

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com