Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,051 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,563 people online right now. Registration is fast and FREE... Join Now!




NameSpaces

 
Reply to this topicStart new topic

NameSpaces, What does 'using namespace std' mean?

krain
3 Aug, 2008 - 05:16 PM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 27

question...
what is Using namespace std. stand for and its usage..please help me.

thanks alot..

User is offlineProfile CardPM
+Quote Post

Whizzy
RE: NameSpaces
3 Aug, 2008 - 05:23 PM
Post #2

Unregistered





QUOTE(krain @ 3 Aug, 2008 - 06:16 PM) *

question...
what is Using namespace std. stand for and its usage..please help me.

thanks alot..


My understanding of the Namespace mechanism in C++ is that it overcomes the problem of name clashes in the global scope. namespace mechanism an application to be partitioned into number of subsystems. Each subsystem can define and operate within its own scope.

Also, you can google (adv search)

c++ namespace

and get 10,000 explainations...



+Quote Post

gabehabe
RE: NameSpaces
4 Aug, 2008 - 02:20 AM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Like Whizzy said, it's basically a container of functions/streams/etc to help prevent name clashes. Let's take a look at how to write a simple namespace:
cpp
#include <iostream>

namespace math
{
double add (double x, double y) {return x + y;} // an add function. pass to parameters to it and
// it will return the value of the two added together
double subtract (double x, double y) {return x - y;} // a subtract function
double multiply (double x, double y) {return x * y;}
double divide (double x, double y) {return x / y;}
} // end of the namespace

// now a few ways to be using our namespace
// we could be "using namespace math;"
// but say if I only want to access the add function
// from our math namespace. What then?
using math::add;
// the :: operator means scope.
// math::add means that "add" is a part of our math namespace

int main ()
{
// since we aren't using namespace std, and we want to access cout, we can still
// do it by adding "std::" in front of cout, meaning the our program needs to search
// in the std namespace to find cout
std::cout << add (4,3) << '\n'; // call our function and add a new line to output
// now, the following line will give an error
// std::cout << subtract (4,3);
// why? because we haven't used subtract, or prefixed it with our scope operator
// however, we could do this:
std::cout << math::subtract (4,3);
}

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: NameSpaces
4 Aug, 2008 - 05:53 AM
Post #4

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
QUOTE(Whizzy @ 3 Aug, 2008 - 06:23 PM) *

QUOTE(krain @ 3 Aug, 2008 - 06:16 PM) *

question...
what is Using namespace std. stand for and its usage..please help me.

thanks alot..


My understanding of the Namespace mechanism in C++ is that it overcomes the problem of name clashes in the global scope. namespace mechanism an application to be partitioned into number of subsystems. Each subsystem can define and operate within its own scope.

Also, you can google (adv search)

c++ namespace

and get 10,000 explainations...


To keep it simple....
it defines basic commands such as
cout << "Hello" << endl;

if you dont type
using namespace std;

you would have to type the aboce like this
std::cout << "Hello" << std::endl;

It makes things simpler

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month