Namespaces in C++

    In a C++ program if we redefine a variable or function with the same name in the same scope then the compiler would throw an error “has a previous declaration”. Example

    #include <iostream>
    using namespace std;
    int main()
    {
        int x= 10;
        double x= 20;
        cout<<x;
    }

    Output (ERROR)

    Compiler Error:
    
    ‘x’ has a previous declaration as 'int x

    Sometimes in a program , a situation may arise when we want to have the same name for two different identifiers such as variables, functions, class, etc., but if we try to initialize or define two different data types with the same name then it thrown an error, so to solve this problem in C++ we have the concept of namespace.

    Namespace

    In the above example, we have used a statement using namespace std; which is an example of namespace concept itself, which we will discuss in the other examples of the namespace. namespace is a keyword that is used to group up the global scope entities such as variable, function, class, and structure and give them a special namespace scope. With the help of namespace we provide a scope to all those identifiers which are present in the namespace block. Namespace was the new feature introduced in C++ and it was not present in the C programming language. namespace must be defined at the global scope. Namespace Syntax:

    namespace namespace_name
      {
        //namespace block
      }

    Namespace Example

    #include <iostream>
    using namespace std;
    
    namespace n_s_1 
       {
         int age = 10;
         int func()
           {  cout<<"Function of Namespace n_s_1"; }
       }               
    
    namespace n_s_2 
       {
          int age= 20;
          int func()
            {
               cout<<"Funciton of Namespace n_s_2";
           }
      }                                                   
    
    int main()
    {
    }

    Behind the Code Here we have only mentioned how can we define a namespace in the program. Here we have defined the two namespaces n_s_1 and n_s_2 , and both have the same attributes age and func().

    Accessing Namespace Attributes:

    In the above example, we have only defined the namespace and its attribute but if we want to access the specific namespace attributes we use the prepend (::) operator with the corresponding namespace. The syntax for accessing namespace attributes:

    namespace_name :: attribute_name;

    Example Let’s use the above example and access the attributes of n_s_1 and n_s_2

    #include <iostream>
    using namespace std;
    
    namespace n_s_1 
       {
          int age = 10;
          int func()
              {  
                   cout<<"Function of Namespace n_s_1";
              }
       }               
    
    namespace n_s_2 
       {
          int age= 20;
          int func()
              {
                 cout<<"Function of Namespace n_s_2";
              }
       }                                                   
    
    int main()
    {
      cout<<"---------Accessing n_s_1 age and func() attribute----------\n";
      cout<<n_s_1::age;
      cout<<endl; //for new line
      n_s_1::func();    // calling the n_s_1 func()      
     
     cout<<"\n---------Accessing n_s_2 age and func() attribute----------\n";
     cout<<n_s_2::age;
     cout<<endl; //for new line
     n_s_2::func();   // calling the n_s_1 func()            
    }

    Output

    ---------Accessing n_s_1 age and func() attribute----------
    10
    Function of Namespace n_s_1
    ---------Accessing n_s_2 age and func() attribute----------
    20
    Function of Namespace n_s_2

    Classes with namespace:

    Using the Namespace we can define a class inside a namespace and using the namespace name, prepend(::) operator and the class object we can access the class properties. Example

    #include <iostream>
    using namespace std;
    
    namespace ns
    {
        // A Class inside a namespace
        class Tech
        {
        public:
            void show()
            {
                cout << "It is the show() method of class Tech\n";
            }
        };
    }
    
    int main()
    {
        ns::Tech obj; // Creating Object of Tech Class
        obj.show();
        return 0;
    }

    Output

    It is the show() method of class Tech

    Ways to access a namespace

    There are two main ways we can use to access the namespace attributes one is the normal way using the prepend operator like we see in the above example the other way is by using directive .

    using directive

    using directive is a technique that can be used to access a namespace without using the prepend operator. In our every C++ program you have noticed the statement using namespace std; this statement is an example of using directive, here using namespace are the two keywords and std is the namespace name which has different attributes such as endl etc. which we use in our program. using directive Syntax

    using namespace namespace_name;

    Example Let’s considered the above example of namespace and access the n_s_1 attribute via using "using directive" method.

    #include <iostream>
    using namespace std;
    
    namespace n_s_1 
       {
         int age = 10;
         int func()
             {  cout<<"Function of Namespace n_s_1"; }
             }               
    using namespace n_s_1;     // using the using directive method for n_s_1 namespace           
    int main()
    {
       cout<<"---------Accessing n_s_1 age and func() attribute----------\n";
       cout<<age;
       cout<<endl; //for new line
       func();                 
    }

    Output

    ---------Accessing n_s_1 age and func() attribute----------
    10
    Function of Namespace n_s_1

    Behind the Code In the above example we did not use the namespace name and prepend operator to access n_s_1 age and func() attributes, here we are simply able to access those by just writing their name, this happens because of the using namespace n_s_1; statement. The using namespace statement defined the value for age and func() identifies.

    Extending Namespace

    If two namespaces have the same namespace name then they both will be treated as the same and they both shear the same scope, which means the second namespace block with the same name would be considered as the continuation of the first namespace block. Example

    #include <iostream>
    using namespace std;
    
    namespace n_s_1 
        {
            int age = 10;
            int func()
             {  
                 cout<<"Function of Namespace n_s_1"; 
             }
        }               
    
    namespace n_s_1 
       {
            int say_hello()
             {
               cout<<"Hello";
             }
       }                             
    
    using namespace n_s_1;     // using the using directive method for n_s_1 namespace           
    
    int main()
    {
        cout<<"---------Accessing n_s_1 age and func() attribute----------\n";
        cout<<age;
        cout<<endl; //for new line
        func();
        cout<<endl;
        say_hello();       
    }

    Output

    ---------Accessing n_s_1 age and func() attribute----------
    10
    Function of Namespace n_s_1
    Hello

    Unnamed Namespace

    If a namespace has no name then it is an unnamed namespace and they are directly useable by the program itself which mean we can access the unnamed namespace attributes directly. Example

    #include <iostream>
    using namespace std;
    
    namespace 
       {
          int age = 10;
          int func(){
            cout<<"Function of Unnamed Namespace"; 
                }
      }                                         
    
    int main()
    {
         cout<<age;
         cout<<endl; //for new line
         func(); 
    }

    Output

    10
    Function of Unnamed Namespace

    Summary

    • The namespace is used to solve the problem of a similar name for two different datatypes.
    • Namespace proved a namespace scope to its attributes.
    • To access the namespace attributes, we have two ways normal way and using directive
    • In the normal way, we use the namespace name and prepend operator to access the namespace attributes.
    • In using the directive technique, we use the use and namespace keywords along with namespace name to define the namespace attribute to the program.

    People are also reading: