All About Namespaces

Anshika Bhargava
The Startup
Published in
3 min readAug 1, 2020

--

Namespaces in C++

When I started competitive coding, a friend of mine asked me to use

using namespace std

before the main code. I started doing that, without actually understanding what it really does. Oh! I knew one thing though. If we do not use that line, we have to write std::cout, std::string etc. So, I followed the template and saved myself from the trouble of that extra writing.

But C++ is awesome, and so are namespaces. So, let us explore this wonderful feature which C++ offers us.

What are namespaces ?

Namespaces are declarative regions for logical groups. Now, I know this may sound complex! So, let me simplify it a little bit.

If we have a set of functions, types and other variables which belong together, we can declare them together under one namespace. This also helps prevent name collisions, like, so,

In the code above, we can access the two print functions as A::print() and B::print() .

How to access members of a namespace

Let us say we want to access the print function of namespace A.

--

--

Anshika Bhargava
The Startup

Software Engineer at Google | I try to learn and blog