CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    1

    Arrow what is diff between #include<iostream> and using namespace std

    Hi everyone

    I am new to c++. In the program why we need both
    #include<iostream> and using namespace std statements.

    what is the difference between these two.

    are cin,endl and cout.. are methods ?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what is diff between #include<iostream> and using namespace std

    I suggest that you read a good beginner's book on C++. I recommend Accelerated C++ if you have a programming background or are a fast learner.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: what is diff between #include<iostream> and using namespace std

    #include <iostream> gives you access to std::cin, std::cout, std::endl, etc.

    using namespace std; allows you to drop the prefix in your code. You could instead have written
    using std::cout;
    using std::cin;
    using std::endl;
    if you preferred.

  4. #4
    Join Date
    Feb 2008
    Posts
    53

    Re: what is diff between #include<iostream> and using namespace std

    Yes, so in short, #include <iostream> loads in certain features from another file so you can use them in your program. Using namespace std allows you to access the members of the std namespace without qualifying them. You'll know what all that means when you learn about namespaces, but basically this allows you to say:
    cout << "Hello, World!"; whereas you'd normally have to say:
    std::cout << "Hello, World!";

    Its just so you don't have to type as much.
    Last edited by Mikau; July 3rd, 2008 at 11:45 AM.

  5. #5
    Join Date
    Feb 2003
    Posts
    377

    Re: what is diff between #include<iostream> and using namespace std

    #include takes the contents of a header file and places where the #include was. (Technically it doesn't have to be a file but in practice it is.) The declarations for different input/output stream objects and classes are in iostream, so when you #include it, you're putting those declarations at the top of your file. When you use those objects, like cout and cin, the compiler needs to know what they are and what they can do. That information is found in the iostream file that you included.

    A namespace is just a way of grouping names so that two things with the same name can be used without conflict. All names in the standard library are in the std namespace. This helps avoid problems when you use names or other code you use has names that are the same as the fairly common names in the standard library.

    So when you use a name from the standard library, you have to tell the compiler that it is in the std namespace. One way to do it (and my preference) is to use the std:: prefix. If you aren't worried about naming conflicts, you can use a using directive to avoid having to type std:: for any name in the std namespace. That is what using namespace std is. It tells the compiler that if it is trying to find what a particular name refers to, it should also check the std namespace to see if it is in there.

    So when you type std::cout you're telling the compiler you want the object called cout in the std namespace. Or if you use the using namespace std line, then when you type cout you are telling the compiler that you want something named cout, which could be in the std namespace. The compiler then looks for the declaration and/or definition of cout, and because you included the information in iostream, the compiler finds the cout there, recognizes that it is the one you want, and uses it.

    Quote Originally Posted by Indianblues
    are cin,endl and cout.. are methods ?
    No. cout and cin are instances of a class (like global variables). I forget what endl is, but I believe it is an instance as well.
    Last edited by jlou; July 3rd, 2008 at 05:13 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what is diff between #include<iostream> and using namespace std

    I forget what endl is, but I believe it is an instance as well.
    The standard manipulators, including std::endl, are functions.

    Incidentally, are you guys really going to explain every single language feature in detail when there are good books out there that do the same thing, but with a more structured approach to learning?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: what is diff between #include<iostream> and using namespace std

    The distinction I explained was one I wondered about myself when I was first learning. That's why I figured it was worth the paragraph.

    But one does have to draw the line somewhere.

  8. #8
    Join Date
    Feb 2003
    Posts
    377

    Re: what is diff between #include<iostream> and using namespace std

    Quote Originally Posted by laserlight
    The standard manipulators, including std::endl, are functions.

    Incidentally, are you guys really going to explain every single language feature in detail when there are good books out there that do the same thing, but with a more structured approach to learning?
    Not everybody is learning from a book. Not every book teaches all concepts in a way that all readers understand.

    I actually would prefer formats like this for clarifying understanding because the nature of a forum allows small misunderstandings to be corrected more quickly.

    Besides, you can't get mad at me, I haven't even posted in weeks.

    P.S. Thanks for the endl clarification.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: what is diff between #include<iostream> and using namespace std

    Quote Originally Posted by jlou
    I actually would prefer formats like this for clarifying understanding because the nature of a forum allows small misunderstandings to be corrected more quickly.
    There is a limit to what is covered in a forum, and what really is covered in great detail in practically all C++ books.

    Also, if the coder is not using a book or books, they are seriously hampering their likelihood of learning C++ properly.

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured