+ Reply to Thread
Results 1 to 4 of 4

Thread: Namespaces: An introduction

  1. #1
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    407

    Namespaces: An introduction

    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:

    Code:
    namespace1::fraction
    namespace2::fraction
    namespace3::fraction
    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
    following.

    Code:
    #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.

    Code:
    #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;
    	return;
    }
    
    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.

    Code:
    #include <iostream>
    #include "newnspace.h"
    using namespace std;
    using namespace newnspace;
    
    int main(void)
    {
    	display();
    	cout << addNums(1, 3) << endl;
    	system("pause");
    }
    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

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,651
    Blog Entries
    57

    Re: Namespaces: An introduction

    Nicely done, though I would discourage adding "using namespace std" in a header file, as it could cause some odd behaviors.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Namespaces: An introduction

    Excellent read! +rep

  4. #4
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    407

    Re: Namespaces: An introduction

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. CSS Part 1: Introduction
    By Lop in forum Tutorials
    Replies: 3
    Last Post: 09-15-2008, 05:24 AM
  2. Introduction
    By Ravenfire in forum Introductions
    Replies: 4
    Last Post: 10-13-2007, 10:09 PM
  3. PHP Introduction
    By clookid in forum PHP Tutorials
    Replies: 10
    Last Post: 01-16-2007, 07:17 AM
  4. HTML Introduction
    By clookid in forum Tutorials
    Replies: 5
    Last Post: 01-08-2007, 09:43 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts