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 email@example.com. 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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
- Why "using namespace std" is considered bad practice
- Namespace in C++ | Set 1 (Introduction)
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- Difference between namespace and class
- Conversion of whole String to uppercase or lowercase using STL in C++
- C++ Program that will fill whole memory
- Print pattern using only one loop | Set 1 (Using setw)
- Count number of unique Triangles using STL | Set 1 (Using set)
- How will you print numbers from 1 to 100 without using loop?
- Print "Even" or "Odd" without using conditional statement
- Can we access private data members of a class without using a member or a friend function?
- How to check whether a number is in the range[low, high] using one comparison ?
- Print individual digits as words without using if or switch
- Database Connectivity using C/C++
- How to find size of array in C/C++ without using sizeof ?
- How to sum two integers without using arithmetic operators in C/C++?
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,