How Does cout Work

This is a discussion on How Does cout Work within the C++ Programming forums, part of the General Programming Boards category; Can someone explain how cout works. I've heard it refers the the standard output stream and it's an object. But ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    How Does cout Work

    Can someone explain how cout works. I've heard it refers the the standard output stream and it's an object. But i'm a bit confused on the object part, probably because i have a limited knowledge of them. The objects i've seen in code and worked with all use the dot operator '.' to access its member functions. If cout is an object (which i'm not douting it is ) how can you use it just as it is, cout?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    cout and cin are objects that have access to the 'queues' for data in your system. They have overloaded operators (which you will learn about if you get deep enough into OOP (speaking of which, does anybody get the joke in my sig? I usually get a couple comments when I change it). The operators are overloaded so you can specify how the data is to be handled.

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I'm still unclear.

    As far as i know, an object creates itself in memory from it's class. But an object is usless by itself. You can't just type the objects name and expect it to do something, you have to access it's member functions for it to do what you want. To me an object has it's dot operator and then it's member function as in
    Code:
    someobj.somfunc()
    Even if the dot operator is overloaded with cout / cin, where is their member functions?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Overloaded operators is how cout and cin working without calling member functions using the dot operator directly. An operator is really just a function, so when you see the operator << in the following statement:

    cout << 3;

    It is really calling a function named operator<<. Since cout has an operator<< member function for each built-in type, that code is the same as the following:

    cout.operator<<(3);

    So you really are calling a member function of cout, it is just more readible and convenient to use the other form of overloaded operators.

    Sometimes, the operator<< is defined as a global function, but even then the cout object is passed to the global function, where its member functions can then be used.

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Wow!!

    I'm so excited. Thanks jlou, i completely understand it now! I did some basic read up on operator overloading and it all makes sense. Cheers
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Another question has come to mind.

    cout is an object, which means it doesn't get defined until the linking process. This is after compilation, so how does the compiler know about cout and not signal an error?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    cout is an instance of an ostream object. Just like all other objects, functions, etc, the compiler does not necessarily need definitions to do its work. It only needs declarations.

    So, if inside the <iostream> header, an instance of ostream is declared named cout, and the ostream class is declared (I'm simplifying it since ostream is really a typedef for basic_ostream<char>), then when your code uses cout, the compiler knows what cout is and what it can do. For example, the ostream class has a declaration for an operator<<, so at compile time the compiler sees that a function operator<< exists that takes an int, and continues compiling happily. When it links, it links the defintion of that function to the place in your code where you called it.

    So the short answer is the difference between declarations and definitions. The compiler doesn't need definitions, only declarations. It doesn't need to know how an object or function works, it only needs to know what it can do.

  8. #8
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    continues compiling happily.


    So, if inside the <iostream> header, an instance of ostream is declared named cout...
    Oh, i never knew an object could be declared. I looked in my iostream header file and i found this:

    Code:
    #ifndef _CPP_IOSTREAM
    #define _CPP_IOSTREAM	1
    
    #pragma GCC system_header
    
    #include <bits/c++config.h>
    #include <ostream>
    #include <istream>
    
    namespace std 
    {
      extern istream cin;
      extern ostream cout;
      extern ostream cerr;
      extern ostream clog;
    #ifdef _GLIBCPP_USE_WCHAR_T
      extern wistream wcin;
      extern wostream wcout;
      extern wostream wcerr;
      extern wostream wclog;
    #endif
    
      // For construction of filebuffers for cout, cin, cerr, clog et. al.
      static ios_base::Init __ioinit;
    } // namespace std
    
    #endif
    is the code in red declaring iostream's objects?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Basically, yes, I believe so.

    But even if it was instantiating the cout object right there inside <iostream>, it would be declaraing it at the same time. You can define something at the same time as you declare it if you like. So if all the compiler could see was the instantiation of an object, that would be enough to use as a declaration to continue compiling happily.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  4. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM

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