A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organise code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
Please refer need of namespace in C++ for details.
Suppose we need two header files for a software project :
C++ Code :
Source code file
Error: Call of overloaded print() is ambiguous.
To overcome this situation, we use namespace objects independently
through scope resolution operator.
Source code file
Advance sub-domains of namespace :https://www.geeksforgeeks.org/namespace-in-c-set-2-extending-namespace-and-unnamed-namespace/https://www.geeksforgeeks.org/namespace-c-set-3-creating-header-nesting-aliasing-accessing/
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
- Namespace in C++ | Set 1 (Introduction)
- Why "using namespace std" is considered bad practice
- Difference between namespace and class
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++