See the Tutorial List

Creating and Using Namespace in C++

Namespace is a container for identifiers. It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace.


Creating a Namespace

Creating a namespace is similar to creation of a class.

namespace MySpace
{
    // declarations
}

int main() 
{
    // main function
}

This will create a new namespace called MySpace, inside which we can put our member declarations.


Rules to create Namespaces

  1. The namespace definition must be done at global scope, or nested inside another namespace.
  2. Namespace definition doesn't terminates with a semicolon like in class definition.
  3. You can use an alias name for your namespace name, for ease of use.

    Example for Alias:

    namespace StudyTonightDotCom
    {
        void study();
        class Learn 
        {  
            // class defintion
        };
    }
    
    // St is now alias for StudyTonightDotCom
    namespace St = StudyTonightDotCom;
    
  4. You cannot create instance of namespace.
  5. There can be unnamed namespaces too. Unnamed namespace is unique for each translation unit. They act exactly like named namespaces.

    Example for Unnamed namespace:

    namespace
    {
        class Head 
        { 
            // class defintion
        };
        // another class
        class Tail 
        { 
            // class defintion
        };
        int i,j,k;
    }
    
    int main() 
    { 
        // main function
    }
    
  6. A namespace definition can be continued and extended over multiple files, they are not redefined or overriden.

    For example, below is some header1.h header file, where we define a namespace:

    namespace MySpace
    {
        int x;
        void f();
    }

    We can then include the header1.h header file in some other header2.h header file and add more to an existing namespace:

    #include "header1.h";
    
    namespace MySpace
    {
        int y;
        void g();
    }

Using a Namespace in C++

There are three ways to use a namespace in program,

  1. Scope resolution operator (::)
  2. The using directive
  3. The using declaration

With Scope resolution operator (::)

Any name (identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution :: operator with the identifier.

namespace MySpace
{
    class A
    {
        static int i;
        public:
        void f();
    };
    
    // class name declaration
    class B;    
    
    //gobal function declaration
    void func();   
}

// Initializing static class variable
int MySpace::A::i=9;      

class MySpace::B
{
    int x;
    public:
    int getdata()
    {
        cout << x;
    }
    // Constructor declaration
    B();   
}

// Constructor definition
MySpace::B::B()   
{
    x=0;
}

The using directive

using keyword allows you to import an entire namespace into your program with a global scope. It can be used to import a namespace into another namespace or any program.

Conside a header file Namespace1.h:

namespace X
{
    int x;
    class Check
    {
        int i;
    };
}

Including the above namespace header file in Namespace2.h file:

include "Namespace1.h";

namespace Y
{
    using namespace X;
    Check obj;
    int y;
}

We imported the namespace X into namespace Y, hence class Check will now be available in the namespace Y.

Hence we can write the following program in a separate file, let's say program1.cpp

#include "Namespace2.h";

void test()
{
    using Namespace Y;
    // creating object of class Check
    Check obj2;
}

Hence, the using directive makes it a lot easier to use namespace, wherever you want.


The using declaration

When we use using directive, we import all the names in the namespace and they are available throughout the program, that is they have a global scope.

But with using declaration, we import one specific name at a time which is available only inside the current scope.

NOTE: The name imported with using declaration can override the name imported with using directive

Consider a file Namespace.h:

namespace X
{
    void f()
    {
        cout << "f of X namespace\n";
    }
    void g() 
    {
        cout << "g of X namespace\n";
    }
}

namespace Y
{
    void f()
    {
        cout << "f of Y namespace\n";
    }
    void g() 
    {
        cout << "g of Y namespace\n";
    }
}

Now let's create a new program file with name program2.cpp with below code:

#include "Namespace.h";

void h()
{
    using namespace X;  // using directive
    using Y::f; // using declaration
    f();    // calls f() of Y namespace
    X::f(); // class f() of X namespace
}

f of Y namespace f of X namespace

In using declaration, we never mention the argument list of a function while importing it, hence if a namespace has overloaded function, it will lead to ambiguity.