Jump to content

Recent Status Updates

  • Photo
      15 Sep

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (2)
  • Photo
      12 Sep

    Just joined. Really enjoy this sight. Excited to become a great programmer, and helper.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
- - - - -

Namespaces: An introduction


  • Please log in to reply
3 replies to this topic

#1 whitey6993


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 156 posts

Posted 27 December 2008 - 02:28 PM

When most of us think of namespaces, our thoughts turn to the statment "using namespace std". This statement, in conjunction with included files like iostream, vector, and the like allow us to use complex data types, functions, and other contents of the standerd library. But although we use this statement all the time, namespaces are still an obscure yet powerful part of C++. In this tutorial, I will explain a bit about namespaces and show you how to create your own. A knowledge of classes and structures is helpful but not required.

In Oriley's C++ in a Nutshell, a namespace is defined as:

A namespace is a named scope.

This means that if you enclose something (like a class) inside a namespace, you can use multiple classes or packages of the same name by using a differnet scope for each package. For example you can write code like this:


The use of namespaces can be useful to both experienced and begging programers. I will explain both of these points in more detail later, but for now, lets look at how to construct a namespace. Namespaces are alot like structures except they can hold functions and are not data types. You set up namespaces like structures as well, and the function deffinitions are the same as function deffinitions in classes. So without any further explination, you are now ready to construct a namespace.

You can place a namespace into one file, but we will be using a header file and an extra .ccp file for this namespace. So first we will construct the header file. open a new header file in your compiler and type in the

#include <iostream>
using namespace std;

namespace newnspace
	void describe(void);
	int addNums(int, int);

The first two lines are the same as in most programs. They are included so that the describe function can be used (shown later). The fourth line is the namespace declaration. All you have to do is type namespace followed by the name of the namespace. Once completed, you fill the namespace with any functions you want to put in it and then close up the bracket. Name this file newnspace.h and save it for later.

For the second file we will use, open a new .cpp file and name it newnspace.cpp. I find it helpful to name my files according to their contents but I do not believe it is neccessary. In this file we will define the functions that we previously set up. So here is what we type into the new file.

#include "newnspace.h"

void newnspace::display(void)
	cout << "This is a namespace" << endl;
	cout << "They are like classes except you do not" << endl;
	cout << "Need to declare an object to access their members" << endl;

int newspace::addNums(int num1, int num2)
	return num1 + num2;

To use the namespace, you have to include the header file, as if you were defining class member functions. First we define display and then we define addNums. Nothing else is needed for this file so we can save it and move on.

The last file we have to write is main.cpp. The code for this is simple.

#include <iostream>
#include "newnspace.h"
using namespace std;
using namespace newnspace;

int main(void)
	cout << addNums(1, 3) << endl;

Notice how you have to include the header file of the namespace and the "using namespace newnspace" statement. Also notice how you can call functions without having to declare an object of the namespaces type. Save and compile all three files and you should see data output to your screen. If this dosen't work, main.cpp and newnspace.cpp example files are attatched for convenience.

Now many beggining programers will not be creating packages or using multiple classes with the same name in a program. So you may be asking "Why would this be practical?" Well let's say that you have a bunch of functions. Functions for manipulating strings, for searching arrays, for sorting arrays, all kinds of reusable functions. Now lets say you want to put them in a class. Well first you have to declare an object of the class type and then use that object to access the classes members. This process is tedious and inconvenient not to mention somewhat of a misuse of classes. If you were to put these functions into an external namespace however, you could just use the namespace and call the functions whenever you want! In longer programs, this will lead to less errors and less code.

In conclusion, namespaces are a powerful part of the C++ language for both beginning and experienced programmers.

The code for this tutorial was written and compiled in Dev-C++ so you may need to alter it to make it work in other compilers. I hope that you enjoyed my second tutorial. If you have any revisions, or would like to contribute to the information, please post a reply.

Attached Files

  • 3

#2 WingedPanther


    A spammer's worst nightmare

  • Moderator
  • 17,043 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 28 December 2008 - 05:58 AM

Nicely done, though I would discourage adding "using namespace std" in a header file, as it could cause some odd behaviors.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

#3 Guest_Jordan_*

  • Guest

Posted 28 December 2008 - 08:07 AM

Excellent read! +rep
  • 0

#4 whitey6993


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 156 posts

Posted 28 December 2008 - 09:29 AM

Thank you for everyone who has posted in this thread and given +rep. I will write another tutorial soon.
  • 0

Also tagged with one or more of these keywords: namespace