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


16 Default arguments in C++
Date: Sep 2, 2018
17 Friend Function, Friend class
Date: Sep 4, 2018
18 What are friend functions ? Is it possible for a function to be friend of two classes ? If yes then how is it implemented in C++. Explain with suitable example. ---PU-2016 (New Course)
Date: Sep 10, 2018
19 Write an OOP to add two complex numbers using passing objects as arguments. ---PU_2015
Date: Sep 10, 2018
20 Write a Program to add two complex numbers using friend function. ----PU_2015
Date: Sep 10, 2018
21 What are constructors and destructors ? Write a Object Oriented Program to display Fibonacci series (Use constructor for initialization) ----PU-2014
Date: Sep 15, 2018
22 Write a OOP to create a class named "Time" with data members 'hour' and 'minute' a constructor to initialize............. ---PU-2011
Date: Sep 15, 2018
23 What is "this" pointer ? Illustrate with example.
Date: Sep 15, 2018
24 Write a program to create class “Counter” having integer data member, overload the post increment, post decrement, pre increment and pre decrement for the class “Counter”.
Date: Sep 15, 2018
25 Write a Program to overload the unary minus operator using friend function.
Date: Sep 15, 2018
26 Write a OOP to overload "==" operator to determine the equality of two fractional numbers (fractional numbers must be in terms of numerator and denominator)
Date: Sep 15, 2018
27 Write a program to create classes “Dollar” and “Rupee”. Write conversion routine to convert dollar to rupee (1 dollar = 108 rupees).
Date: Sep 15, 2018
28 Write a Program to convert angle in degree to angle in radian. where, angle in radian = angle in degree * PI/180. Use Conversion routine in source class.
Date: Sep 15, 2018
29 Write a program which asks file name from keyboard, opens a file with that name for output, reads the line from keyboard character by character and writes the line onto the file.
Date: Sep 22, 2018
30 WAP to to read the text from the file named "hello.txt" to the file name called "test.txt".
Date: Sep 22, 2018

Tools/Apps





© Nepal Exchange Rates