code for Using Namespace in C++
Using Namespace in C++

Before you start

you must see our previous post My first C++ program

Start Learning about Namespace:

In c++ , when a variable function or class is used in a  particular scope then duplicacy is not allowed i.e no two class, variable or a function can have a same name with in same scope . But when the scope expands i.e in global scope same names can be used . This may cause collision while linking as which entity of two same entites should be used .

So, to overcome this problem namespace is used

When namespace is not used then error is generated , this is so because during compiling the linker gets confused as to refer to which function .

Capture1

 

Now when namespace is used  the compiler reads this print function as abc::print() and abcd::print() i.e both print () are now easily distinguishable . Now the compiler can easily link the functions. Here a seperate container is created i.e namespace and each container contains its own functions variable or a class which are easily distingishable .

 

Capture2

 

Also generaly we use using namespace std , if this is not used then then we need to write cin cout as std::cin or std::cout. Basically our program is like a almirah, and namespace is just like drawers  in it . If we wont use namespace then linker will not know from where to use the function , i.e until or unless you know the location of the things you want or that you are searching you cant find them . So after defining namespace linker can easily find function to link . Here too on defining namespace std we are refereing to standart functions of the drawer std , else if namespace is not used then it wont have anything to refer to and error will be generated.

 

subscribe by register Buffercode to learn more about programming languages and please wait for next post.

 Previous                                                                                                                       Next

What to do next:

NO COMMENTS