Namespaces in C++. What? Why? and How?
Suppose you work in a Software company and you are currently working on a project in C++ along with one of your other colleagues. You were assigned a task for the day to add some extra functionality to your colleague’s code and for that, you need to create a function in his code. You finally think of a suitable name for that function that will describe the function perfectly, But now you discover that your colleague already has a function by that name.
Huh! 🙄. So either you can argue with your colleague about changing the name of that function, but that won't be civil.
Or there’s another way to keep your function’s name while leaving your colleague’s function unhurt. That’s where Namespaces come into play.
A namespace can be seen as a block or space where whatever identifiers you use, they won't interfere with the identifiers (names of functions, variables or any other quantity) used outside of that space. Because it is just a block, the variables or functions defined inside are of local scope and cannot be accessed outside directly.

So what you do is, You create your own block and give it a name (a Namespace) and write whatever code you want in there. Now it will not collide with the names of identifiers defined outside that block. Sounds simple right? Yes, it is.
Also, the identifiers used inside the namespace can be accessed anywhere in the program with the Scope Resolution Operator (::).

Here is an example of namespaces. We have 2 variables and 2 functions with the same name and with different values. Let us see what will happen if we print both of them.

As you can see that the variable defined inside the namespace and the variable defined in the main function does not collide because of having the same name because they don't have the same scope now. The same goes for the function defined inside the namespace and the one defined outside it.
Namespaces are very important in C++ as they can help localize your code instead of making it global and it can also prevent a fight between you and your colleagues over identifiers!😉
Happy Coding!