What is "namespace" and why do we use it?

What is namespace?

"In general, a namespace is a container for a set of identifiers   Namespaces provide a level of direction to specific identifiers, thus  making it possible to distinguish between identifiers with the same  exact name. "
Or
According to WikipediaIn computer programming namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisions between multiple identifiers that share the same name.

Why we need namespaces?

Assume there is two libraries named A and B, both have get_data function in it. You included these two libraries in your code. Now you call the get_data function, then compiler won’t know which one to call, and you’ll get an error. Now how we can fix this issue.

 In C lang we can fix by adding a prefix to start of every function like A_get_data and B_get_data,which is painful. 
The solution of this problem comes in C++ in the form of namespaceA namespace is a more flexible way of adding prefixes. So you can name the functions A::get_data and B::get_data. The difference is that if you are going to use bunch of code of A library then you can add “using namespace A” at the top of your file, and then just call get_data without using A prefix for the rest of code.It saves on typing. Another difference is that you can leave off all the prefixes while writing code inside the namespace itself, which again makes things easier to read.

The std namespace is special; it is short for the word "standard." The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc. Because these tools are used so commonly, it's popular to add "using namespace std" at the top of your source code so that you won't have to type the std:: prefix constantly. And because these functions are kept in a namespace, if you really want to use "vector" as a variable name, you still can. Namespaces give you more freedom to use short, accurate names.

'using namespace std' is not necessary to mention.

Drawbacks of ‘using namespace’

It forces all includers of that header file to use that namespace, which might result in naming ambiguities that are hard to fix. This practice is called namespace pollution. Instead, always use the fully prefixed names in header files (std::string not string) and save the using declarations for the source files.

1. #include <iostream>2. using namespace std;3. 4. namespace first5. {6.     int val = 500;7. }8.9. namespace second10. {11.    int val = 100;12. }13.14. int main()15. {16.    17.    int val = 200;18.    cout << first::val << '\n';19.    cout << second::val << '\n';20.    cout << val << '\n';21.22.    return 0;23.  }
Output:

Namespace Decleration:

namespace namespace_name {   int x, y; // code declarations where              // x and y are declared in              // namespace_name's scope}
➧ Namespace declarations appear only at global scope.

➧ Namespace declarations don’t have access specifiers. (Public or private)

➧ No need to give semicolon after the closing brace of definition of namespace.

Read this How to create own header file in C++?


 Follow us on
                              
For more details...

You may also like:
C++: Pointers

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

C++: STL Iterators