Thread: C++ cout

  1. #16
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    I know what you mean. I usually hide the source code and only show the user the program. I make the code look really big and complicated when I'm trying to get a friend to start on C++ from HTML.
    Adam

  2. #17
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    namespaces are useful if you make a function library. Their purpose is to reduce name clashes. Also it helps a human reader understand what a function or variable is for if they are grouped together under a common name. i.e all graphics functions could be grouped under a gfx namespace...

    Code:
    namespace gfx {
        void Initialize();
        void Render();
        void Shutdown;
    }
    Instead of

    Code:
        void gfx_Initialize();
        void gfx_Render();
        void gfx_Shutdown();
    Of course you wouldn't make the gfx scope globle because Initialize, Shutdown and Render are popular names for functions. So you would use the scope operator.

    Code:
    gfx::Initialize();
    When calling a function.

    Namespaces are open, i.e.

    Code:
    namespace bob {
        int x, y;
    }
    
    namespace bob {
        std::string name;
    }
    
    namespace bob {
        int age;
    }
    You can keep adding names whenever you want.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is the explanation I plan to put in my tutorial. I suppose this is as good a time as any to test its understandability. (Opinions, please!)

    std::cout is kind of a weird name, what with the double colon in the middle. You'll also notice that I kept referring to the object as just cout in the previous section.
    There are two kinds of names - the technical term is identifier - in C++. Local names, which are simple, like cout. And global names, which in their full form look like this:
    ::path::to::identifier
    However, the compiler is capable of guessing at parts of this construct, so you can usually get away with not using the full name. For example, it is very often possible to leave out the initial ::.
    So what is this all about?
    Good identifiers are not available in unlimited numbers. At some point, two people might want to give their objects the same name. And if these two people then go on to combine their code into one program, they have a problem. This problem even has a name: it's called name collision.
    The usual solution to name collision is to make the names longer. Most C libraries tend to have a common prefix for everything they provide. For instance, the zlib compression library prefixes everything with a single z. The jpeglib image compression library prefixes everything with jpeg_.
    This works. However, it can become tedious to write the prefix over and over, even when you know that you don't need it in the code you're writing.
    Namespaces are the answer. The idea is to use a language construct to automatically give all identifiers from a library a common prefix. Since the compiler knows what part is the prefix, it is possible to omit it under some circumstances. We will discuss these circumstances later.
    The entire C++ standard library uses the prefix std. cout therefore is the local name of the object within the std namespace, referred to as ::std::cout or, because the initial :: usually can be omitted, std::cout.
    No mention of using statements or declarations yet. That'll come later in the tutorial, as I want to start out with good practice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    could just include stdafx.h and never have to type std:: ever

  5. #20
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    ok, let me see if I got this right...

    the std namespace is part of the <iostream>...

    and it's optional if I use :: or not... no matter how many namespaces I have included?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Code:
    #include <iostream>
    
    int main()
    {
    
          cout <<"Hello World!\n";
    
    return 0;
    }
    it works for me

    You dont need namespace someone should of just told him this in the first place

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You do need the namespace, that code does not work on most compilers and is illegal C++. If it works for you, fine, but you are picking up a bad habit that will screw things up when you switch compilers.
    Last edited by Daved; 09-16-2005 at 12:01 PM.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Devil Panther,

    Ignore the comment about :: for now. CornedBee was talking about the first :: in ::std::cout. You cannot ignore the middle :: in std::cout.

    The std namespace is used by all standard library headers in C++. This includes <iostream>, <fstream>, <string>, <vector>, <cstdlib>, <cstdio>, <cmath>, and many others. To use the things from those headers, you have to tell the compiler that the thing you are using is from the std namespace. There are different ways of doing this.

    One is to tell the compiler to always check the std namespace for any name you use (this is the using namespace std; option).

    Another is to tell the compiler that any time you use a specific name, to check the std namespace (this is the using std::cout; option).

    Another is to tell the compiler every single time that the name you are using is from the std namespace (this is the std::cout option).

    There are different ways to use those options as well. The point is that standard library names live in the std namespace, and if you want to use one of them you have to tell the compiler to look there for them.

  9. #24
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by HaVoX
    Code:
    #include <iostream>
    
    int main()
    {
    
          cout <<"Hello World!\n";
    
    return 0;
    }
    it works for me

    You dont need namespace someone should of just told him this in the first place
    what compiler do you use?!!! since you do need it with the advance ones!!! it's not Dev C++ that includes things without you knowing!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #25
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Daved
    Devil Panther,

    Ignore the comment about :: for now. CornedBee was talking about the first :: in ::std::cout. You cannot ignore the middle :: in std::cout.

    The std namespace is used by all standard library headers in C++. This includes <iostream>, <fstream>, <string>, <vector>, <cstdlib>, <cstdio>, <cmath>, and many others. To use the things from those headers, you have to tell the compiler that the thing you are using is from the std namespace. There are different ways of doing this.

    One is to tell the compiler to always check the std namespace for any name you use (this is the using namespace std; option).

    Another is to tell the compiler that any time you use a specific name, to check the std namespace (this is the using std::cout; option).

    Another is to tell the compiler every single time that the name you are using is from the std namespace (this is the std::cout option).

    There are different ways to use those options as well. The point is that standard library names live in the std namespace, and if you want to use one of them you have to tell the compiler to look there for them.
    1) i'm not really sure about the difference between the last two.
    2) so you're saying that the above includes use the std...
    3) what's with the ::std::cout ?

    thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #26
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    example 1
    Code:
    using namespace std;     // pull in everyting from namespace std
    cout << i << endl;       // no std:: needed because compiler looks for everything in namespace std
    cin >> i;                // like above
    example 2
    Code:
    using std::cout;          // pull in only cout from namespace std
    cout << i << std::endl;   // no std:: needed for cout because compiler looks for cout
    std::cin >> i;            // std:: is needed because compiler looks only for cout
    example3
    Code:
                                   // no using declaration
    std::cout << i << std::endl;   // std:: needed because nothing is pulled in 
    std::cin >> i;                 // like above
    Kurt

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    3) Ignore the ::std::cout. Basically there is a global namespace that you access with the first ::, but rarely do you need to worry about it.

    2) Yes. In C++, all standard library headers put all the names into the std namespace. There are lots of standard library headers besides the examples I gave.

    1) Here are examples of the three options I mentioned:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Hello World" << endl;
        return 0;
    }
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout << "Hello World" << endl;
        return 0;
    }
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World" << std::endl;
        return 0;
    }

  13. #28
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I see... but why std what does it mean? Can I write things to address the std ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you read the FAQ on namespaces? Did you understand CornedBee's explanation of a namespace above? std is just a namespace. The people who standardized the language decided to put the standard library stuff into a namespace, and they decided to call it std (for standard).

    You'll have to do some reading elsewhere if the explanations in this thread aren't enough to explain namespaces.

  15. #30
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    std is the standard functions.

    Let me simplify even further - and use VB to explain.

    In VB, I might have a function that is native to the language such as Open for opening files for read/write. In VB, if I type in Open, open, OPEN, OpEn, or any combination of cased letters, the VB IDE would correct the case to Open and now that it's a restricted keyword. In debugging a VB application, if I try to step into the line that says Open, I don't see anything. The Open word doesn't take me to a deeper level.

    C++ is case sensitive. Restricted keywords are not auto-corrected for case. IF and if are viewed as two different objects, the first being some sort of variable and the latter is part of an if/then statement. If I am stepping through a C++ program and come upon specific "keywords," the debugger will take me to definitions of those words in their original header files. (note, I might be a bit off in my description but conceptually it should be close.)

    std is a huge namespace of common functions/processes typically used in a c++ program. HOWEVER, the "power" of c++ is the compiler only grabs what is absolutely necessary to run the program. VB, on the otherhand, says grab anything that COULD be needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM