C++ Namespace Introduction:

Multiple programmers may work for single C++ program. In which all they may work on separate files. When these are combined their may be same identifier in multiple files. Then compiler will generate errors. And at this time it is much difficult to trace and debug these error.

In C++ a variable can only be declare for single data type.

Syntax of namespace: 

Syntax of defining namespace in C++ is as follows.

namespace identifier {

// lines of code

}

Accessing Namespace Elements:

We can access elements of namespace using scope resolution operator (::). Such as namespace_identifier::element.

CPP Namespace Example:

#include <iostream>

// Standard namespace
using namespace std;

// userdefined namespace xyz
namespace xyz {
    int a = 10;
}

// userdefined namespace abc
namespace abc {
    int a = 20;
}

int main(int argc, char** argv) {
    
    // accessing element of xyz namespace
    cout<<"Element of xyz Namespace: "<<xyz::a<<endl;
    
    // accessing element of abc namespace
    cout<<"Element of abc Namespace: "<<abc::a<<endl;
    
    return 0;
}

In this example we have defined two namespaces named as abc and xyz. Each namespace containing one element, which is a variable of int data type. When we want ot access variable of xyz namespace we use scope resolution operator with namspace and its element.

Output: 

The output of above namespace source code will be as.

Element of XYZ Namespace: 10
Element of XYZ Namespace: 20

Namespace Directive:

We can also use namespace directive to access all elements of single namespace. For this purpose we have to write using namespace directive before accessing its elements. In this method we can skip namespace identifier and scope resolution operator before accessing individual elements. This is also convinient way to access namespace in C++.

To use multiple namespace directive in main() function we must use opening and closing delimeters for each namespace.

Example:

#include <iostream>

// Standard namespace
using namespace std;

// userdefined namespace abc
namespace abc {
    void func() {
        cout<<"Function of abc namespace"<<endl;
    }
}

// userdefined namespace xyz
namespace xyz {
    void func() {
        cout<<"Function of xyz namespace"<<endl;
    }
}

int main(int argc, char** argv) {
    
    // using namespace directive
    
    using namespace abc;
    func();
    
    // without using namespace directive
    xyz::func();
    
    return 0;
}

Output:

Function of abc namespace
Function of xyz namespace

Standard Namespace std:

CPP also provide standard namespace, this namespace is used for standard input and output operations. The cin and cout are standard input and output objects. We can use these object with namespace directive or directly using scope resolution operator.

Example:

#include <iostream>

int main(int argc, char** argv) {
    
    // without using namespace directive
    std::cout<<"Hello World"<<std::endl;
    
    // using standard namespace directive
    
    using namespace std;
    cout<<"Hello C++ Namepsace"<<endl;
    
    return 0;
}

Output: 

Hello World
Hello C++ Namepsace

CPP namespace alias:

We can also give new name to existing namespace according to reqirement. This is an alternative name that may be more readable and short.

Syntax:

namespace new_identifier = existing_namespace;

 

Comments
Login to TRACK of Comments.