[C++ Tutorial 4] More on Namespaces


To see an introduction to what namespaces are, go back to tutorial 3. Now, cout is a function in the namespace “std”. Now, there’s another way of using cout without specifying the namespace before hand.

<span style="color: #000099;">#include&lt;iostream&gt;
</span><span style="color: #ff6633;">int</span><span style="color: #990000;"> main</span><span style="color: #663300; font-weight: bold;">(

std<span style="color: #663300; font-weight: bold;">::</span>cout<span style="color: #663300; font-weight: bold;">&lt;&lt;</span><span style="color: #009900;">"Hello Jolly!"</span><span style="color: #663300; font-weight: bold;">;</span>
system<span style="color: #663300; font-weight: bold;">(</span><span style="color: #009900;">"pause"</span><span style="color: #663300; font-weight: bold;">);</span><span style="color: #ff0000;">
return</span><span style="color: #999900;"> 0</span><span style="color: #663300; font-weight: bold;">;

Here, std::cout means the cout that comes from the std namespace. However, since we would eventually use a large number of output and input statements(ie cout’s and cin’s), we would usually declare the namespace beforehand.

Creating our own namespaces

We can also create our own namespaces. These may contain a number of variables and functions to be used for the particular namespace. We here create a namespace called “my” with a variable of integer type called x.

We will be going into variables and data types in a few tutorials, but here’s ow you create an integer variable called x-

<span style="color: #ff6633;">int</span> x<span style="color: #663300; font-weight: bold;">;</span><span style="color: #999999; font-style: italic;">   //creates a variable called x
           //which takes integral values</span>

So, here’s our own namespace-

<span style="color: #990000;">namespace</span> my<span style="color: #663300; font-weight: bold;">{</span><span style="color: #999999; font-style: italic;">   //creating a namespace
</span><span style="color: #ff6633;">int</span> x<span style="color: #663300; font-weight: bold;">;
}</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><span style="color: #663300; font-weight: bold;">()
my<span style="color: #663300; font-weight: bold;">::</span>x<span style="color: #663300; font-weight: bold;">;</span><span style="color: #999999; font-style: italic;">         //using the namespace variable
</span><span style="color: #663300; font-weight: bold;">}</span>

If we intend to use our variable x a number of times, we could specify our namespace in the program.

<span style="color: #990000;">using namespace</span> my<span style="color: #663300; font-weight: bold;">;</span><span style="color: #999999; font-style: italic;">  //declare namespace beforehand</span>

Get the best of Durofy in your inbox


About Author

Founder at Durofy. Blogs on technology & startups, runs a full service digital agency, teaches AP Calculus.


  1. Pingback: Durofy » C++ Tutorial 3 – Basic Program Structure