<< complete newb :)

This is a discussion on << complete newb :) within the C++ Programming forums, part of the General Programming Boards category; Im just wondering, whats the difference between std::cout and cout I've seen both in some sources, but I don't understand ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    2

    << complete newb :)

    Im just wondering, whats the difference between

    std::cout

    and cout

    I've seen both in some sources, but I don't understand them. And what does :: do? : ) thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    :: is the scope resolution operator.

    std::cout means to use the cout that resides in the std namespace
    cout means to use the cout in whatever namespace you are in

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    2
    ok then, keep in mind that im completely new to c++, i program vb but this is way different

    whats a namespace ?

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Instead of prefixing every function call with std::, you could put "using namespace std" after the includes. I think I read about a disadvantage to that somewhere, but I cant remember what.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The disadvantage is that nothing in the global namespace (or another imported namespace) can then have the same name as anything you have included in whatever namespaces that you have done away with by using the 'using' keyword.

    Safer, is to say 'using std::cout;', 'using std::endl;', etc for only the identifiers you need. Safer still, is to stick with just using the namespace identifier for scope resolution when you use the identifiers.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    thats how i see it:

    imagine you develop a class library, and share it with your costumers etc, so they can use your library.
    imagine you make a class that is part of your library called Cars, ie:
    class Cars{
    //code here
    };

    i install your library in my computer, but you'r not so lucky because other guy made another class that i have installed in my pc and he calle it Cars too, ok if pretend to use that class it will not do what i want because i both clases collide.

    how can you prevent this? when u develop a set of classes is a good programming to put it
    into namespaces ie

    namespace terra
    {
    //here i put all my code and clases ie
    class Cars{
    //code here
    };
    }

    ok know when u share your library your users to make use of the Cars class they must use
    terra::Cars
    or intead of typing everytime terra::something you define the namespace ie:
    using namespace terra;
    know you dont need to type terra::

    then the user can start to create objects with that class with no problems
    ie. Cars toyota;

    if you r more lost after reading my post avoid it, because my english sucks

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Im just wondering, whats the difference between

    std::cout

    and cout

    I've seen both in some sources, but I don't understand them.
    What they all said above.

    But wait, I also something to add! Some of the older compilers require you to include <iostream.h>, which is the nonstandard way of including it (i.e. the old way). If you include the .h version, then there is no std:: in front of cout, and you don't put 'using namespace std;' - because it's not declared/defined in a namespace in the old headers.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    This is also similar to my question about what \n does. What's the difference between no \n and \n. What does \n do?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    \n is the newline character. As the name suggests it causes a newline to be outputted

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>What's the difference between no \n and \n. What does \n do?
    >>>As the name suggests it causes a newline to be outputted

    Generally true, except for in Windows applications, where it causes a funny square to be outputted unless you use a "\r\n" combo. Also, I think writing a '\n' to a file opened in text mode will actually output a "\r\n" unless you open the file in binary mode

    P.S. You might want to get a good C++ book, or look over the tutorials on this site...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also, I think writing a '\n' to a file opened in text mode will actually output a "\r\n" unless you open the file in binary mode
    Not if you open it in a linux enviroment.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oops, yeah. That's just a Windows thing, 'cause Microsoft said so
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Well, newline is just a character, and it's interpretation is application-specific (or so I imagine). If you so wished, you could make a text editor or console which treated as a newline character instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout << SunTradingLLC << "is looking for C++ Software Developers" << endl;
    By sun trading in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 04-02-2008, 11:48 AM
  2. Complete Newb, have a loop that won't exit
    By jason414 in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2006, 08:38 AM
  3. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  4. Doom Lord engine complete!
    By Leeman_s in forum Game Programming
    Replies: 8
    Last Post: 05-12-2002, 12:44 AM
  5. doom lord engine complete!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 04-25-2002, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21