Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials
Register Blogs Search Today's Posts Mark Forums Read

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-27-2008, 05:28 PM
whitey6993's Avatar
Programming Professional
 
Join Date: Dec 2008
Location: In front of my Computer
Posts: 291
whitey6993 has a spectacular aura aboutwhitey6993 has a spectacular aura about
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:
Quote:
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
File Type: cpp main.cpp (196 Bytes, 8 views)
File Type: cpp newnspace.cpp (342 Bytes, 10 views)
__________________
"Simplicity is a cornerstone of genius. Have you ever heard anyone say 'I love that idea, its so complicated!'"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-28-2008, 08:58 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-28-2008, 11:07 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Namespaces: An introduction

Excellent read! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-28-2008, 12:29 PM
whitey6993's Avatar
Programming Professional
 
Join Date: Dec 2008
Location: In front of my Computer
Posts: 291
whitey6993 has a spectacular aura aboutwhitey6993 has a spectacular aura about
Re: Namespaces: An introduction

Thank you for everyone who has posted in this thread and given +rep. I will write another tutorial soon.
__________________
"Simplicity is a cornerstone of genius. Have you ever heard anyone say 'I love that idea, its so complicated!'"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS Part 1: Introduction Lop Tutorials 3 09-15-2008 06:24 AM
Introduction Ravenfire Introductions 4 10-13-2007 11:09 PM
PHP Introduction clookid PHP Tutorials 10 01-16-2007 08:17 AM
HTML Introduction clookid Tutorials 5 01-08-2007 10:43 PM


All times are GMT -5. The time now is 08:22 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0