15
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--defining same identifiers again

Embed Size (px)

Citation preview

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 <iostream>using namespace std;

namespace first_space // first name space{ void func()

{ cout << "Inside first_space" << endl; }}namespace second_space // second name space{ void func()

{ cout << "Inside second_space" << endl;}

}

int main (){ first_space::func(); // Calls function from first name space.

second_space::func(); // Calls function from second name space

}

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 <iostream>using namespace std;

namespace me{ int value()

{ return 5;

}}

namespace me1{

const double pi = 3.1416;double value() {

return 2*pi; }

}

int main () { cout << me::value() << '\n'; cout << me1::value() << '\n'; cout << me1::pi << '\n';}

Namespaces can be split :

◦ 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<iostream> 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<iostream>

using namespace std;

namespace { int local; }

int main()

{ local = 1; cout << "Local=" << local << endl; return 0; }

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 :

// this is h1.h file and u need to save it as “.h “namespace {

int local1; // as it is anonymous, the namespace is visible only upto this file

}

void func() {

local1 = 2; }

// this is main file

#include<iostream>

#include"h1.h"

using namespace std;

void func();

namespace {

int local=1; }

int main()

{

local =3;

local1=4;

cout << "Local=" << local << endl;

func();

cout << "Local1=" << local1 << endl;

return 0;

}

Namespace aliasing :

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:

#include<iostream>

using namespace std; // this namespace is for endl and cout

namespace m

{

int local=1;

}

namespace s=m; // creating alternate name for namespace

int main()

{

cout << "Local=" << s::local << endl;

cout << "Local=" << m::local << endl;

return 0;

}

Output:

11

C++ has a default namespace named std, which contains all the default

library of the C++.

USING A NAMESPACE

There are 3 ways to use a namespace in program.

Scope resolution

with “ Using” directive

with “ Using” declaration

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 :

#include<iostream>

using namespace std;

namespace m { int local = 1; }

namespace n {

char a='A'; }

int main() {

int local=2;

cout << local << endl;

cout << m::local << endl; // scope resolution

cout << n::a << endl;

return 0; }

2.WITH “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 visible 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.

#include <iostream>using namespace std;

namespace first{ int x = 5;}

namespace second{ double x = 3.1416;}

int main () { { // using directive in 1st block using namespace first; cout << x << '\n'; }

{ // using directive in 2nd block using namespace second;

cout << x << '\n'; }

return 0;}

Name imported with using declaration can override the name imported

with using derivative

#include <iostream>using namespace std;

namespace first{ void f() { cout<<"this is fun 1F";

}

void g() { cout<<"this is fun 1G";

}}

namespace second{ void f() { cout<<"this is fun 2F";

}

void g() { cout<<"this is fun 2G";

}}

int main () {

using namespace first; // using directive

using second::f; // using declaration

f(); // calls f() of second namespace return 0;}

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 <iostream>using namespace std;

namespace name1{ void func()

{ cout << "Inside name1" << endl; } namespace name2

{ void func()

{ cout << "Inside name2" << endl; } }}

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