Solved

about namespaces

Posted on 2000-03-01
5
223 Views
Last Modified: 2013-11-19
what are the namespaces in c++? what is the importance of them? can we create user defined namespaces?
0
Comment
Question by:maheshhatolkar
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
namespaces are "scopes" in which the symbolic names you create in your source code are placed.  All names within a namespace must be unique, but names in seperate namespaces may be duplicated.

continues
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
In a function, you cannot have two local variables with the same name.  However two seperate functions may have variables that have the same name, like in

void f1()
{
   int X;
   double X; // Error.
}

there is an error because the same name is used twice but in

void f1()
{
   int X;
}

void f2()
{
   double X;
}

there is no error bacuase the two names are iin seperate function, i.e. seperate scopes.

continues
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
A namespace works the same way.  It is a scope in which you can define templates, classes, constants, enums and other symbolic names.  Those names must be unique within that namespace but may be used in other namespaces. like

namepace vehicles
{
    class plane
    {
    };
};

namespace geometry
{
    class plane
    {
   };
}

the two classes defined are distinct classes buth they both can have the same name because they are defined in seperate namespaces.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> what is the importance of them?
They are used to prevent naming conflicts.  This is especially important when dealing with code libraries.  For example the STL library defines a class caled "string"  Well string is a VERY popular name.  If that name was not placed in a namespace it would conflict with the name "string" used in other circumstances.  But the name is placed in a namespace called "std"

Note to use a name that is defined in a namespace, you must preceed the name with the namespace name and a scope resolution operaotr ("::")  Like to use the STL string, you woudl specify "std::string".  Another option is to include the std::string name into the current namespace using

using std::string;

Then you don't have to specify the "std::" before "string"  Finally you could all the names defined in the std namespace into the current namespace using a single statement like

using namespace std;

>> can we create user defined namespaces?
Absolutely.
0
 

Author Comment

by:maheshhatolkar
Comment Utility
Thanks neitod, i got really a good answer.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now