Suppose
In u r class, if there are two persons with same name
vinod,
and if u want to
call one person, how will u call him ?? Whenever
we need to differentiate them (in
case of more members ) definitely
we would have to use some additional information along with their
name, like surname
or the area (if
they live in different area)
or their mother
or father name, etc.
Same
situation arises here. Suppose in u r code , If u had declare the
function with name say abc(), and there is some other library
available with the same function name and if u include that library
in u r code , Then the compiler has no way of thinking which xyz()
function u r referring in the code. In order to overcome this
difficulty Namespace is introduced.
Namespace:
Namespace is a container
for a
set of identifiers
(names of variables, functions
, classes) . It
puts the names of its members in a distinct space so that they don't
conflict with the names in other namespaces or global namespace.
For
example :
1.
|
|
namespace myname
{
int a, b;
}
|
|
|
In
this case, the variables a
and b
are normal variables declared within a namespace called
m
yname.
These
variables can be accessed from within their namespace normally, with
their identifier (either a
or b
),
but if accessed from outside the m
yname
namespace they have to be properly qualified with the scope operator
::
.
For example, to access the previous variables from outside m
yname
namespace
they should be qualified like this:
Example 2.
output
:
Inside first_space
Inside second_space
The concept can be
depicted using the following diagram:
Generally most of them
will use the standard namespace with the following declaration :
using
namespace std;
It tells that in the
same namespace two variables are declared seperately. Ofcouse u might
think why declaring seperately in same file, this will help a lot
while declaring the same namespace in different files.
Take
a look with an example :
Here
, as there is no name for namespace , we won't be using the scope
operator
It
also avoids making global static variable.
Take a look with an example
:
It
is also possible to declare an alternate name for an existing
namespace.
We
can use the following format:
namespace new_name = current_name;
For
example:
1
1
USING
A NAMESPACE
There are 3 ways to use a namespace in program.
1.
SCOPE RESOLUTION :
From
the name itself u have got some idea ..
Any
name (identifier) declared in a namespace can be explicitly specified
using the namespace's name and the scope resolution ::
operator with the identifier.
Take
a look with an example :
2.
W
ITH
“USING” DECLARATION :
The
keyword using
introduces a name into the current declarative region (such as a
block) .
With
this using declaration we can import a specific name in that block .
Take
a look with an example :
#include
<iostream>
using
namespace std;
namespace
first
{
int
x = 5;
int
y = 10;
}
namespace
second
{
double
x = 3.1416;
double
y = 2.7183;
}
int
main ()
{
using
first::x;
// using declaration
using
second::y;
// using declaration
cout
<< x << '\n';
cout
<< y << '\n';
cout
<<
first::y
<< '\n';
cout
<<
second::x
<< '\n';
return
0;
}
Suppose
if u
put
using declaration
in a block, then it will be vi
s
ible
only in that block.
U
can access the namespace from other files also.
Try
yourself this one and check the output.
int
main ()
{
{
using
first::x;
using
second::y
;
}
cout
<< x << '\n';
cout
<< y << '\n';
return
0;
}
3.WITH
USING DIRECTIVE :
With using keyword u can import only one
variable at a time.But with using derivative , allows you to import
an entire namespace into the program with global scope . It can be
used to import a namespace into another namespace or any program.
#include
<iostream>
using
namespace
std;
namespace
first
{
int
x = 5;
int
y = 10;
}
namespace
second
{
double
x = 3.1416;
double
y = 2.7183;
}
int
main ()
{
using
namespace
first;
//
using
directive
cout
<< x <<
'\n';
cout
<< y <<
'\n';
cout
<<
second::x
<<
'\n';
cout
<<
second::y
<<
'\n';
return
0;
}
using
and using
namespace
have validity only in the same block in which they are stated or in
the entire source code file if they are used directly in the global
scope. For example, it would be possible to first use the objects
of one namespace and then those of another one by splitting the
code in different blocks.
Name
imported with using declaration can override the name imported
with using derivative
Nested
Namespaces :
Namespaces
can be nested where you can define one namespace inside another name
space as follows:
namespace name1
{
namespace name2
{
fun();
}
}
One can access members of nested
namespace by using scope operators as follows:
using namespace name1::name2; // to access members of name2
using namespace name1; // to access members of name1
Take
a look with an example :
The result of this program should be ???