Results 1 to 5 of 5

Thread: newbie question

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Location
    USA
    Posts
    10

    Question

    Hi,

    I am learning C++ and I am using VC++ 6.0. In some of the books I read, they will start a program with something like this:

    #include <iostream.h>

    using std::cout;
    using std::cin;
    using std::endl;

    My question is that, what is the purpose of listing all the functions that you are going to use? Is it really necessary because my program still compile and run without all those "using" statements. Is it because VC has done that for me or something?

    Thank you very much.

    nina.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You could do it like this:
    Code:
    #include <iostream> 
    using namespace std;
    
    //then
    int main()
    {
    cout<<"something"<<endl;
    cin>>varname;
    //so on
    Other compilers won't let you do this

    #include <iostream.h>

    but you should include the libraray

    #include <iostream>
    //here you need std with every cout or cin Or just once with using namespace std;
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Just to clarify - <iostream> is the standard library, whereas <iostream.h> is the Microsoft modified version of the standard library. I don't think there's any real advantage to using the MS library, except you don't need to add using namespace std with it. Better to use the standatrd library really.
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Location
    USA
    Posts
    10

    Smile

    Thanks for the response. But you still didn't answer my question (may be because I didn't state my question clear enough). That is:

    Why does the C++ book shows
    using std::cout;
    using std::cin;
    using std::endl;
    when the program run perfectly okay without them?

    What are the purpose of those "using std" statements? I understand the Source File Inclusion (i.e. #include <> ), but I don't understand the "using std" statements.

    Thank you very much.

    nina.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The using directive imports names from one namespace to another. If you're not in a namespace, then you're automatically in the global namespace, where everything could be preceded by :: if used.

    So, what using does, is to allow you to use a name from one namespace in another:
    Code:
    namespace parksie {
        int x;
    }
    
    using parksie::x;
    
    void main() {
        parksie::x = 5;
    
        cout << x << endl; // Prints "5"
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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