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


31 Write a program to enter the name and salary for a number of employees, store them all in a file, and finally search and display the salary...............
Date: Sep 22, 2018
32 Write a program, using a class to read the contents of the text file and convert all upper case letters to lower case and vice-versa and write the result in to another file.
Date: Sep 22, 2018
33 Write a program to enter the id and price of a number of products, store them all in a user defined file, and search the product when particular id is entered.
Date: Sep 22, 2018
34 Write a program using class template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
35 Write a program using function template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
36 Write a program using function template to find the sum of first and last element of an array.
Date: Sep 23, 2018
37 Write a template function to find the maximum number from the template array of size N.
Date: Sep 23, 2018
38 Write a function template to swap the numbers.
Date: Sep 23, 2018
39 Write a program using class template to swap two set of numbers.(one integer set and another floating point set)
Date: Sep 23, 2018
40 Exception handling in C++
Date: Aug 11, 2019
41 Explain the concept of encapsulation and information hiding In an object oriented paradigm with the help of an example.
Date: Sep 2, 2019
42 Write a program to find sum of first and last element of an array demonstrating use of new and delete operator.
Date: Sep 2, 2019
43 Write a program to compare two integer numbers and two single character using function overloading.
Date: Sep 2, 2019
44 Write an oop to create a class named “Time” with data objects and two member functions to add two member functions to add two times and display the resultant time.
Date: Sep 2, 2019
45 Write a program to define a class “string” using constructor initialize two string objects with given strings and write necessary functions to concatenate and display the strings.
Date: Sep 2, 2019

Tools/Apps





© Nepal Exchange Rates