Namespace

A program includes many identifiers defined in different scopes. Sometimes an identifier of one scope will overlap (i.e. collide) with an identifier of the same name in a different scope, potentially creating a problem. Identifier overlapping also occurs frequently in third-party libraries that happen to use the same names for global identifiers (such as functions).


To solve this problem, we use the concept of the namespace. Each namespace defines a scope where identifiers are placed. The general form of the namespace is:
namespace namespace_name 
{ 
Namespace body (variables, functions, classes, etc.)
}
To use a namespace member, either the member’s name must be qualified with the namespace name and a binary scope resolution operator (::), as in
namespace_name :: member

or a using statement must occur before the name is used. The general form in this case is
using namespace namespace_name;

In this case, all the members of the namespace namespace-name can be used directly by their names. We can also use only the specified members by using “using the statement” as follows
using namespace_name::  member;

One example of the namespace is the C++ standard library (std). All classes, functions, and templates are declared within the namespace name std.

Example:
#include<iostream>
#include<conio>
namespace TestSpace
{ 
int m;
void display(int n)
{  
std :: cout< 

}
}
void main()
{ 
TestSpace :: m = 8; 
TestSpace :: display(TestSpace :: m);  
}

The same program can be written as follows:

#include<iostream>
#include<conio>
using namespace std;
namespace TestSpace {
int m; 
void display(int n)
{  
Cout<<n; 
}
}
using namespace TestSpace;
void main()
{ 
m = 8; 
display(m);
 }

We can again write the same program as follows:

#include<iostream>
#include<conio>
using std :: cout;
namespace TestSpace
{ 
int m; 
void display(int n)
{  
cout< 

}
}
using TestSpace :: m;
using TestSpace :: display;
 void main()
{ 
m = 8; 
display(m);
}

 Note: Like in class, we can also declare a function inside the namespace and define it outside. For example,

namespace TestSpace
{ 
int m; 
void display(int);
}
void TestSpace :: display(int n)
{ 
cout<<"You entered "<<n;
}
Nesting Namespaces A namespace can be nested within another namespace as follows: namespace Outer
{
 ……
……
namespace Inner
{
…… 
 …… 
} 
……
……
}

Example:
 #include<iostream>
#include<conio>
 using namespace std;
namespace TestSpace
{
 int m = 100;  
void display()
{  
Cout<<m; 
} 
namespace InnerSpace
{  
int m = 200;  
void display()
{   
c o u t << m ;
   }
  }
}
 void main()
{ 
TestSpace :: InnerSpace :: display(); 
}

Output:

200


Alternatively, we can write

using namespace TestSpace :: InnerSpace;   
display();
Or,
using TestSpace :: InnerSpace :: display;
display();

Unnamed Namespace An unnamed namespace is one that does not have a name. Unnamed namespace members occupy global scope and are accessible in all scopes following the declaration in the file. The members, in this case, can be accessed without using any qualification. A common use of unnamed namespace is to protect global data between files. 
Example:

#include<iostream>
#include<conio>
using namespace std;
namespace
{ 
int m; 
void display(int n)
{  
Cout<<"You entered "<<n; 
} 
} void main()
{
clrscr();  cout<<"m = "; 
cin>>m;   
display(m); 
getch();
}

Using Classes in the Namespace

We can also define classes inside the namespace. For example,

#include<iostream>
#include<conio>
namespace NS
{
class rectangle
{  
private:   
int length, breadth;  
public:   
rectangle(int l, int b)
{    
length = l;    
breadth = b;   
}    
 int findarea()
{   
 return 2 * length * breadth;   
}
 };
}
void main()
{
 clrscr(); 
using namespace std; 
NS :: rectangle r1(5, 3);
 Cout<<"Area = "<<r1.findarea(); 
getche();
}

Output:

Area = 30

Alternatively, we can write

using namespace NS;
rectangle r1(5, 3);
Or
using NS :: rectangle;
rectangle r1(5, 3);




Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours