Namespaces in C++ Programming

Namespace is used to define a scope where identifiers like variables, functions, classes, etc are declared. The main purpose of using a namespace is to prevent ambiguity that may occur when two identifiers have same name.
For example, Run is an English word which can have different meanings that vary according to the context, like His running won him a gold medal at the Olympics and He's running a movie theater. Here, meaning of the some additional information/context is provided to prevent the ambiguity.

Likewise, in C++, if there are two same named functions say test() performing different tasks. In order to prevent ambiguity while calling one of the two functions, we can declare them in two separate namespaces. Suppose, one of these function is declared in namespace name1 and another is declared in name2. Then we can call them separately as name1::test() and name2::test().

C++ uses a standard namespace called std where all the built-in classes and functions are declared. So, we use the statement using namespace std; at the beginning of our C++ program to use members defined in namespace std. 

C++ Namespace Definition

Namespace definition is similar to class definition, the only difference is it doesn't end with a semicolon like a class does. namespace keyword is used at the beginning like class is used while defining a class

Syntax of Namespace Definition

namespace namespace_name
{
    body of namespace
}

The member can be accessed in the program as,

namespace_name::member_name;

For Example,

namespace sample
{
    int a;
    int sum(int x, int y)
    {
        return (x+y);
    }
}

Now, the variable and function sum() can be accessed in the program as,

sample::a = 5;

int x = sample::sum(5,9);

Nested Namespaces in C++

Defining a namespace inside another namespace is called nested namespace.

Syntax of Nested Namespace

namespace namespace_name1
{
    body of namespace_name1
    ... ... ...
    namespace namespace_name2
    {
        body of namespace_name2
    }
    ... ... ...
}

The member declared inside nested namespace (namespace_name2) can be accessed as,

namespace_name1::namespace_name2::member_name;

Example of Nested Namespace

1. C++ program to define nested namespace.

#include<iostream>
#include<conio.h>
using namespace std;

namespace name1
{
    int a;
    float b;
    namespace name2
    {
        float add(int x, float y)
        {
            return x+y;
        }
    }
}

int main()
{
    cout<<"Enter a and b: ";
    cin>>name1::a>>name1::b;
    cout<<"Sum = "<<name1::name2::add(name1::a,name1::b)<<endl;
    getch();
    return 0;
}


Nested namespace is used in this program. A namespace name2 is defined inside another namespace name1. The members of name1 i.e. a and b can be accessed simply as name1::a and name1::b. But to access the members of name2 i.e. add(), we need to specify both namespaces name1 and name2. The add() function can be accessed as name1::name2::add() as shown in program.

Output

Enter a and b: 13 27.892
Sum = 40.892

C++ Unnamed Namespace

A namespace without any name is called unnamed namespace. C++ allows programmer to create unnamed namespaces. After defining an unnamed namespace, its members can be accessed from any module of the program without using any qualification. They are usually created to shield global data.

Syntax of Unnamed Namespace

namespace
{
    body of unnamed namespace
    ... ... ...
}

Example of Unnamed Namespace

2. C++ program to define unnamed namespace.

#include<iostream>
#include<conio.h>
using namespace std;

namespace
{
    void display(int x)
    {
        cout<<"Value = "<<x;
    }
}

int main()
{
    int n;
    cout<<"Enter an integer: ";
    cin>>n;
    display(n);
    getch();
    return 0;
}

In this program, a unnamed namespace is defined which consists of a function display() that displays an integer passed through parameter.

Output

Enter an integer: 13
Value = 13

The using directive

Keyword using is used to inform the compiler that the variables, classes and functions declared inside a namespace is being used in the program. By doing so, we don't need to attach the name of namespace before the members of that namespace.

Syntax of using directive

using namespace namespace_name;

Example of using directive

3. C++ program to illustrate the use of using keyword.

#include<iostream>
#include<conio.h>
using namespace std;

namespace sample
{
    class person
    {
        private:
            char name[100];
            int age;
            char gender[10];
        public:
            void getdata()
            {
                cout<<"Name: ";
                cin>>name;
                cout<<"Age: ";
                cin>>age;
                cout<<"Gender: ";
                cin>>gender;
                cout<<endl;
            }
            void display()
            {
                cout<<"Name: "<<name<<endl;
                cout<<"Age: "<<age<<endl;
                cout<<"Gender : "<<gender<<endl<<endl;
            }
    };
}

int main()
{
    sample::person p1;    // namespace name is required
    using namespace sample;
    person p2;            // namespace name is not required
    cout<<"Entering data for person p1"<<endl;
    p1.getdata();
    cout<<"Entering data for person p2"<<endl;
    p2.getdata();
    cout<<"Displaying data for person p1"<<endl;
    p1.display();
    cout<<"Displaying data for person p2"<<endl;
    p2.display();
    getch();
    return 0;
}

A class person is defined inside a namespace sample. The attributes of person is name, age and gender and it has two functions, getdata() and display(). Inside main(), two objects of person are created. Before using the 'using namespace sample;' statement, we need to specify the name of namespace before class to create an object. But after using the using 'namespace sample;' statement, we no longer need to specify the name of namespace while using its members. 

Output

Entering data for person p1
Name: William
Age: 34
Gender: Male

Entering data for person p2
Name: Sarah
Age: 27
Gender: Female

Displaying data for person p1
Name: William
Age: 34
Gender : Male

Displaying data for person p2
Name: Sarah
Age: 27
Gender : Female