-
What does 'using namespace std' mean in C++?
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 myname.
These variables can be accessed from within their namespace
normally, with their identifier (either a or b), but if accessed
from outside the
myname namespace they have to be properly qualified with the
scope operator
::. For example, to access the previous variables from outside
myname they
should be qualified like:
myname::a
myname::b
Example 2.
#include using namespace std;
namespace first_space // first name space{ void func()
{ cout
-
}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;
We can access functions and variables with the scope operator in
the
same way.
#include using namespace std;
namespace me{ int value()
{ return 5;
}}
namespace me1{
const double pi = 3.1416;double value() {
return 2*pi; }
}
int main () { cout
-
Two segments of a code can be declared in the same namespace
i.e.
We can have more than one namespace of the same name. This
gives
the advantage of defining the same namespace in more than one
file
(although they can be created in the same file as well).
#include using namespace std;
namespace mynamespace { int x; }
namespace mynamespace { int y; }
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.
We can have anonymous namespaces (namespace with no name).
They
are directly usable in the same program and are used for
declaring unique
identifiers.
Take a look with an example :
#include
using namespace std;
namespace { int local; }
int main()
- { local = 1; cout
-
{
local =3;
local1=4;
cout
- cout
-
Take a look with an example :
#include
using namespace std;
namespace m { int local = 1; }
namespace n {
char a='A'; }
int main() {
int local=2;
cout
-
#include
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
-
int main ()
{
{
using first::x;
}
using second::y;
cout
-
}int main ()
{
using namespace first; // using directive
cout
- cout
-
Nested Namespaces :
Namespaces can be nested where you can define one namespace
inside another name space as follows:
namespace name1
{
namespace name2 // namespace name2 is in the block ofnamespace
name1
{ 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 :
#include using namespace std;
namespace name1{ void func()
{ cout
-
int main (){
using namespace name1::name2;
func(); // This calls function from name2 namespace.
return 0;}
The result of this program should be: ???
FOR REFERENCES :
http://comsciguide.blogspot.in/2014/12/namespace_26.html
Namespace:USING A NAMESPACE1. SCOPE RESOLUTION :2.WITH USING
DECLARATION :3.WITH USING DIRECTIVE :Nested Namespaces :