≡ Menu

How to use C++ Namespaces with an Example Code

When we talk about namespaces, we should understand certain things about one important operator from the world of C++.

That one important C++ Operator is “::”

When you confront a C programmer with “::” operator, he might look at it but not understand it.

It is one of the things that distinguish the C++ world from C world.

Before we talk about the technical aspect of :: operator, let us first understand conceptually what this means.

If you have same city name in different countries, even when the city name is exactly the same, we still know both these cities are different, as they are from different countries.

In the same way, you have entities like classes or namespaces, and you have something that could be used in the situations where you have same names in different entities.

If you have class CA with method “method”, and if you are developing the class that has the method which you are coding outside the brackets of that class, it becomes very useful. So, you could use it like this:

CA::method(whatEverArgumentsYouNeed);

Well, you could ask a question “Why do I need this?”

The following are some of the reasons why you should use namespaces:

  • To use two variables with the same name in your code.
  • You might have few classes, definitions, constants, etc. Very often those things can be handled way better, if you unite them into something that you will call namespace. Similar to uniting multiple cities to a country.
  • Multiple programmers could use two different variables with same name, but if they put them into different namespaces you have dissolve the ambiguity that might arise from the case of two variables with the same name.

To use the namespace, do the following:

OneNamespace::TheVariable

If you execute the following program, it will throw few error messages:

#include <iostream>
//using namespace std;
int main()
{
  cout<<"Hello World!!!"<<endl;
  return EXIT_SUCCESS;
}

Now, you could change the hello world line of code into:

std::cout<<"Hello World!!!"<<std::endl;

The above is basic example of a namespaces. In this example, we use the std namespace, which is known as the standard namespace.

In other words, as soon as you write using a namespace std, you have the ability to work with: ostream or istream. Those two classes are child classes of an ios class but they are basics for an iostream class.

Next, we will create two namespaces with same name for variable, const and function.

#include <iostream>

using namespace std;

namespace First
{
const double cdPDV=3.2139;
int i= 10;
void Function(void){ cout<<"In first namespace"<<endl;}
};

namespace Second
{
const double cdPDV=5.43899;
int i=20;
void Function(void){ cout<<"In the second namespace"<<endl;}
};

int
main()
{
	cout<<"Using namespaces!!!"<<endl;

	cout<<"From the first one"<<endl;
	cout<<First::cdPDV<<endl;
	cout<<First::i<<endl;
	First::Function();

    cout<<"From the second one"<<endl;
	cout<<Second::cdPDV<<endl;
	cout<<Second::i<<endl;
	Second::Function();

	return EXIT_SUCCESS;
}

In the above code, we have created one namespace with name First, and second one with the name Second. You have organized your constants, variables and functions into one namespace and now you could even create separate file for each of them and use them as you wish.

If you don’t like to use the First and the Second, it is possible to avoid that, using namespace NameOfTheNamespace. After you have written that, you don’t need to write NameOfTheNamespace::Something, it is enough to write Something.

It is possible to separate the main function with brackets. This way, from one point in your code, you could use one namespace, but when you don’t need that namespace any more you can close the brackets, and from that point on-wards, you are able to define new namespace. In that case, you don’t have the need to write “OtherNameSpace::Something”, just “Something” is enough.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • duskoKoscica May 10, 2014, 11:56 am

    Operator “::” is called the scope resolution operator, and it would be good to look it up in table to se priority, etc…

  • Tito J. Puente May 12, 2014, 1:06 am

    Cool.

    It looks like tihis is good for classes and other things.

    I like it. I like it a lot.

  • Tito J. Puente May 28, 2014, 5:08 am

    It is cool!
    But if I get it right, this mean that this name space thing will be usefull in classes.