Re: what does using namespace std mean?

Discussion in 'C++' started by Peter van Merkerk, Aug 21, 2003.

  1. "soni29" <> wrote in message
    news:...
    > hi,
    > i did some c++ back in college, currently work mainly with java, and
    > i'm trying to refresh myself in c++, after starting a tutorial online
    > i noticed that the author used the line using namespace std. what is
    > that? i've never seen that in college or in my c++ text. i see the
    > site has the #include<iostream.h> to bring in cin and cout but not
    > sure why the std line is there. the code the site had is:
    >
    > #include <iostream.h>
    > #include <string>
    >
    > using namespace std;
    > int main() {
    > string name;
    > cout << "What is your name?";
    > cin >> name;
    > cout << "Welcome, " << name << ".";
    > return 0;
    > }


    Namespaces are used to avoid name clashes with other libraries. C++
    namespace are _somewhat_ similar to Java packages. Standard library
    class and functions reside in the std namespace. Without the 'using
    namespace' directive you would have to explicitly add std:: before every
    identier of the standard library:

    int main() {
    std::string name;
    std::cout << "What is your name?";
    std::cin >> name;
    std::cout << "Welcome, " << name << ".";
    return 0;
    }

    The 'using namespace std;' directive pulls the identifiers in the std
    namespace into the current namespace so that you don't have to
    explicitly specify that you are refering to a identifier from the std
    namespace. The 'using namespace' directive should be used judiciously in
    the smallest posible scope and never in header files. To liberal use of
    'using namespace' directives may lead to unexpected results.

    BTW. <iostream.h> is non-standard header it should be <iostream>. If the
    tutorial can't get these simple things right one might wonder about the
    quality and accuracy of rest of the tutorial

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Aug 21, 2003
    #1
    1. Advertising

  2. Peter van Merkerk

    soni29 Guest

    hi,
    thank you for the response, you have posted this code:

    int main() {
    std::string name;
    std::cout << "What is your name?";
    std::cin >> name;
    std::cout << "Welcome, " << name << ".";
    return 0;
    }

    if i use this method of std:: then there should be no need for the
    #include<iostream> correct, as long as all i'm doing is cin and cout?
    are std library and the <iostream> same? sorry i don't have the api
    on me, i'm currently using a unix c++ complier, from college, and i
    don't see and api around on it that the college is providing us. i'll
    try searching the web, but thought i'd case incase i can't find my
    answer.

    Thank you again.

    "Peter van Merkerk" <> wrote in message news:<bi2eeo$3pm1g$-berlin.de>...
    > "soni29" <> wrote in message
    > news:...
    > > hi,
    > > i did some c++ back in college, currently work mainly with java, and
    > > i'm trying to refresh myself in c++, after starting a tutorial online
    > > i noticed that the author used the line using namespace std. what is
    > > that? i've never seen that in college or in my c++ text. i see the
    > > site has the #include<iostream.h> to bring in cin and cout but not
    > > sure why the std line is there. the code the site had is:
    > >
    > > #include <iostream.h>
    > > #include <string>
    > >
    > > using namespace std;
    > > int main() {
    > > string name;
    > > cout << "What is your name?";
    > > cin >> name;
    > > cout << "Welcome, " << name << ".";
    > > return 0;
    > > }

    >
    > Namespaces are used to avoid name clashes with other libraries. C++
    > namespace are _somewhat_ similar to Java packages. Standard library
    > class and functions reside in the std namespace. Without the 'using
    > namespace' directive you would have to explicitly add std:: before every
    > identier of the standard library:
    >
    > int main() {
    > std::string name;
    > std::cout << "What is your name?";
    > std::cin >> name;
    > std::cout << "Welcome, " << name << ".";
    > return 0;
    > }
    >
    > The 'using namespace std;' directive pulls the identifiers in the std
    > namespace into the current namespace so that you don't have to
    > explicitly specify that you are refering to a identifier from the std
    > namespace. The 'using namespace' directive should be used judiciously in
    > the smallest posible scope and never in header files. To liberal use of
    > 'using namespace' directives may lead to unexpected results.
    >
    > BTW. <iostream.h> is non-standard header it should be <iostream>. If the
    > tutorial can't get these simple things right one might wonder about the
    > quality and accuracy of rest of the tutorial
     
    soni29, Aug 22, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Fraley
    Replies:
    1
    Views:
    4,530
    Kevin Goodsell
    Aug 21, 2003
  2. Dave O'Hearn
    Replies:
    3
    Views:
    4,270
    Agent Mulder
    Aug 23, 2003
  3. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,399
    Howard
    Nov 29, 2004
  4. Replies:
    7
    Views:
    438
    Jacek Dziedzic
    Oct 3, 2006
  5. Jeffrey Walton
    Replies:
    10
    Views:
    956
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page