Namespace in C++.

Aug 17, 2018

As program grows larger in c++ programming; gets more sophisticated; needs more than one programmer to accomplish the project. When there is more than one programmer involved for the same project, there is a possibility of naming of variables, function names, and even classes with the same name. And it is not an easy task to manage that which and what naming convention should one use. Eventually at the time of program integration there can be naming conflicts in the programs; and the namespace concept comes to solve the problem.

 The namespace mechanism in c++ is is used for the logical grouping of the variables, functions, and calasses. The related program elements actually can be put into a same namespace. It, thus helps to get ride of naming conflicts between the modules, packages of the c++ programs designed and written by the different members of the same programming team. Actually, the namespace provides the scope for the identifiers; and the same identifiers name can be used in different modules of the programs. The scope resolution operator :: can be used to point or denote the specific identifiers of the modules.

The namespace keyword is used to define the namespace. The syntax is given as :

namespace namespace_name{
//body of the programs
//declaration of variables, functions, and classes
}// no semicolon at the end

 

The defined namespace can be used in our progam by using using keyword. Syntax is :

using namespace namespace_name;

//for example we are using std namespace in c++ programming
//as shown below for direct access of the cin and cout

using namespace std;

//if the above statements is not declared in the program,
//and if we are about to use cin and cout we have to use as

std::cin>>x; //reading x from console
std::cout<<"hello"; //displaying hello to console

 

Example:

In this example, the two namespaces ABC and DEF are defined and the same identifiers are used

#include
using namespace std;

// defination of ABC namespace
namespace ABC{
	int variable_name = 10;
	void display_variable(){
		cout<<"I am from namespace ABC and i have variable with value:"<<variable_name<<endl;
	}
}

// defination of DEF namespace
namespace DEF{
	int variable_name = 20;
	void display_variable(){
		cout<<"I am from namespace DEF and i have variable with value:"<<variable_name<<endl;
	}
}

int main(){

	ABC::display_variable();
	DEF::display_variable();
	cout<<"Variable value in ABC:"<<ABC::variable_name<<endl;
	cout<<"Variable value in DEF:"<<DEF::variable_name<<endl;
	return 0;
}

Output:

I am from namespace ABC and i have variable with value:10
I am from namespace DEF and i have variable with value:20
Variable value in ABC:10
Variable value in DEF:20

 

Nesting namespce

A namespace can contain other namespaces inside it. The general syntax is:

namespace namesoace_name{

    //program codes

    namespace namespace_name_1{

        //program codes

    }

}

 

example on nested namespace:

#include
using namespace std;

// namespace defination
namespace OUTER{
	int number = 100;
	void display(){
		cout<<"outer namespace variable:"<<number<<endl;
	}
	namespace INNER{
		int number = 200;
		void display(){
			cout<<"inner namespace variable:"<<number<<endl;
		}
	}
}
int main(){
	OUTER::display();
	OUTER::INNER::display();
	cout<<"Outer variable :"<<OUTER::number<<endl;
	cout<<"Inner Variable :"<<OUTER::INNER::number<<endl;
	return 0;
}

output:

outer namespace variable:100
inner namespace variable:200
Outer variable :100
Inner Variable :200

 

Unnamed namespace:

Every elements: variables, functions, and variables has namespace scope even if they are not declared. They have unnamed namescope. The unnamed namescope can be globally accessed. Eventhough the elements not defined within the namespace scope have unnamed name scope, the unnamed namespace can also be defined without specifying the namespace name.

namespace {

    //body of the namespace

}

 They are globally accessible means they need not be included using using keyword.

 

 

Related


Tools/Apps





© Nepal Exchange Rates