Page 1 of 1

C++ Namespaces Working with them, and creating your own! Rate Topic: ***** 2 Votes

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 06 August 2008 - 12:56 PM

Working with namespaces

What is a namespace?
A namespace is basically a container for a group of variables/functions etc. I can pretty much guarantee everyone reading this tutorial has already used a namespace in C++ programming. Think of a line that usually comes at the top of a beginner program:
using namespace std;
This is the standard namespace. It's got the definitions of a lot of neat stuff in there.

Why should I use a namespace?
Namespaces are pretty neat. We use them to contain a group of data. Also, it makes it possible to have more than one variable with the same name. One of which is in the namespace, one which isn't. This will be explained later on.

When writing a namespace, there's nothing you need to include. A namespace is a standard container.

I'm gonna put my namespace in a seperate header file, just to save confusion.

Let's start with our header guards. I'm gonna call the header file "namespace.h" but you can call it whatever you like.
Also, I'm just gonna define it in the .h file. Granted, this is bad habit, but this is just a quick example.

#ifndef NAMESPACE_H_INCLUDED
#define NAMESPACE_H_INCLUDED

Now we're ready to start writing a namespace. I'm gonna write a simple "math" namespace, since I've used something similar in a few posts to explain it already, and the people who I wrote it for found it quite useful.

There isn't too much to writing a namespace, but it's a useful skill. There isn't too much to break it down into, so I'm gonna post up a simple namespace here. This is namespace.h:
#ifndef NAMESPACE_H_INCLUDED
#define NAMESPACE_H_INCLUDED

namespace math // define a namespace called math
{
    namespace numbers // an example of a nested namespace
    { // define some variables within our math::numbers namespace
        int one = 1;
        int two = 2;
        int three = 3;
        int four = 4;
        int five = 5;
        int six = 6;
        int seven = 7;
        int eight = 8;
        int nine = 9;
    } // end of nested namespace

    // define some functions within our math namespace
    int add (int x, int y) {return x + y;}
    int subtract (int x, int y) {return x - y;}
    int divide (int x, int y) {return x / y;}
    int multiply (int x, int y) {return x * y;}
}

#endif // NAMESPACE_H_INCLUDED

As you can see, I've even made a nested namespace. You can nest namespaces as far as you like, but it seems a little pointless to have something as long as:
namespace1::namespace2::namespace3::namespace4::x just to access a variable named x.

And now that I've said that, you're probably thinking "what's this :: operator that this fool uses?"
The :: operator is known as the scope operator, and it is used to access certain things from within a namespace. The most common, which you may have seen before is something like:
std::cout << "something";
This line basically means that cout is defined in the std namespace. Makes sense, right?

Onwards to the various methods of accessing a namespace! This can be done with various methods.
There are three methods of accessing something from within a namespace. I'm gonna use the std namespace as an example here:
  • using namespace std; This means that we want to be using everything which is defined within the std namespace
  • using std::cout; This means that we only want to use cout from the std namespace
  • This last method doesn't require the using keyword. We use it whenever we want to access something from the namespace. Example:
    #include <iostream> // input/output stream
    
    int main ()
    {
        std::cout << "Enter something!" << std::endl;
        char input;
        std::cin >> input;
        std::cout << "You entered: " << input << "!" << std::endl;
        std::cin.get (); // pause
        return EXIT_SUCCESS;
    }
    
Notice how we had to prefix every use of any std feature with std:: ? This can become tedious, which is why we have the using keyword! :)

Now for our main.cpp file, which is used to demonstrate everything we've learned so far:
#include <iostream>

#include "namespace.h"

// using a namespace
using namespace math; // using everything from the namespace called numbers
// (our nested namespace)

// using individual members of a namespace
using std::cout; // use cout from the std namespace specifically
using std::cin;  // use cin from the std namespace specifically
using std::endl; // use endl from the std namespace specifically

int main ()
{
    cout << math::add (numbers::three, numbers::two) << endl;
    // note: if we weren't using the math namespace, we would have to prefix three and two with:
    // math::numbers::three and math::numbers::two
    // this is why it's kind of advisable against using huge nested namespaces!
    
    // now, since we need to access two through numbers::two we can still have another variable called two
    // because of the scope.
    int two = 771123;
    // now let's output both "two" variables and see what we get
    cout << "two = " << two << endl
         << "numbers::two = " << numbers::two;

    cin.get ();
    return EXIT_SUCCESS;
}


Since it's so simple, I've decided a basic example added to the end of this tutorial would suffice. I have just written some basic code, which demonstrates the use of a nameless namespace. Here it is:
#include <iostream>
#include <string>

using std::string; // string datatype
using std::cout; // output to the console
using std::cin; // input from the console

namespace
{
    string str = "hello"; // create a string, which is stored in our nameless namespaces
}

int main ()
{
    // remember how we access things from within a namespace?
    // we prefix it with the name of the namespace, then the scope operator
    // since we don't have a name, all we need to do is prefix it with the
    // scope operator, like so:
    cout << ::str; // output the string
    cin.get (); // hold the window open
    return EXIT_SUCCESS; // successful execution
}

Told you it was simple!

Happy coding!

Is This A Good Question/Topic? 4
  • +

Replies To: C++ Namespaces

#2 darklogan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 08

Posted 31 August 2008 - 10:19 AM

Great tutorial, but there is still something I dont understand... why is it that when using namespaces you don't have to use the .h? i mean, you just wrote

#include <iostream>

instead of

#include <iostream.h>

?? Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 01 September 2008 - 06:54 AM

Nicely done.
:^:
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 01 September 2008 - 12:33 PM

Thanks b2c :)

@darklogan:
What? You never need to add .h to iostream, it's C++ standard to deprecate it~
Was This Post Helpful? 0
  • +
  • -

#5 Newby_one_Canuby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-August 08

Posted 08 December 2008 - 09:51 PM

:) Thats pretty Cool ! it kind of open a wide area to use when you declare each item in your namespace you must show its attributes or methods? yes?

If you dont want to put it in the .h file where do you put it?
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 12 December 2008 - 06:14 AM

You can just put it in your main file. Doesn't matter where you put it, really. I just put it in a separate file because I try to keep my work organised (and it's a good practice to preach)

:)
Was This Post Helpful? 1
  • +
  • -

#7 trucho++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-September 09

Posted 16 September 2009 - 12:33 AM

so theres no need to includ #include "namespace.h" either right?
Was This Post Helpful? 0
  • +
  • -

#8 Arpit_Patel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 12

Posted 22 April 2012 - 09:05 PM

View Postdarklogan, on 31 August 2008 - 10:19 AM, said:

Great tutorial, but there is still something I dont understand... why is it that when using namespaces you don't have to use the .h? i mean, you just wrote

#include <iostream>

instead of

#include <iostream.h>

?? Thanks in advance


View Postdarklogan, on 31 August 2008 - 10:19 AM, said:

Great tutorial, but there is still something I dont understand... why is it that when using namespaces you don't have to use the .h? i mean, you just wrote

#include <iostream>

instead of

#include <iostream.h>

?? Thanks in advance


Ans:- When ever you are using turbo c at that time you have to use header files like <iostream.h>
but when you are useing visual studio c++ compiler at that time no need to define ".h"
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Posted 30 April 2012 - 04:58 AM

View Posttrucho++, on 16 September 2009 - 01:33 AM, said:

so theres no need to includ #include "namespace.h" either right?


The namespace is part of C++, no additional #includes are necessary.
Was This Post Helpful? 0
  • +
  • -

#10 tony12095  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 109
  • Joined: 23-October 12

Posted 24 October 2012 - 08:55 PM

View Postdarklogan, on 31 August 2008 - 05:19 PM, said:

Great tutorial, but there is still something I dont understand... why is it that when using namespaces you don't have to use the .h? i mean, you just wrote

#include <iostream>

instead of

#include <iostream.h>

?? Thanks in advance


There is honestly no need for a .h in a c++ file, it is only in C that you subdue the .h
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1