tags:

views:

126

answers:

4

Hii ,

I am relatively new to programming C++ . I do know that the functions cout , cin etc... are defined in the standard name space . But we also include iostream header file for running the program .

So , is it like

 namespace std
   {

     declaration of cout 

     declaration of cin 

     ..... some other declarations etc....

   }

and theior actual implementations inside istream and ostream ... ????

Or , is it the other way round ...??? like ....

namespace std 
   {
     complete definition of cout 
     complete definition of cin  
     .........

   } 

and their signatures are just placed in the iostream file like ...

iostream file 
  {
    std :: cout 
    std :: cin 
    .....

  }

Please provide any examples or links that you might think will help me understand better

+1  A: 

cin and cout are not simple variables - with cerr they are streams registered by default for every application using iostream. you can't use them without including that header.

Tomasz Kowalczyk
+2  A: 

The standard really doesn't say. It's entirely possible for the implementer to do it as a header-only library, but it's much more likely for them to just put the declarations in headers and put the implementations in the CRT.

EDIT: However, the definitions for cin, cout, etc need to be extern and defined in some sort of library. (See UncleBens' answer)

Billy ONeal
+1  A: 

To use cin and cout you really only have to know that they're in the std namespace and that you need to include iostream to use them.

To give you an idea how it might be implemented, the definition of class std::ostream could be in the header ostream, which is included by iostream. Also in the header ostream, std::cout could be defined as a reference to an std::ostream.

James Hopkin
+4  A: 

I do know that the functions cout , cin etc... are defined in the standard name space.

These are not really functions, but global instances of basic_ostream and basic_istream.

But we also include iostream header file for running the program.

You rather include headers so you can compile your source (the compiler needs to declarations etc).

The rest of the question is rather fuzzy. How the standard library is implemented is pretty much up to the implementation. The standard requires that if you include iostream, you will get the declarations of the following globals:

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;

  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;
}
UncleBens
+1 -- though I was not aware that they had to be `extern`...
Billy ONeal
@Billy: without `extern` these would be definitions rather than declarations, and you don't want definitions of external objects in a header.
Mike Seymour
@Bens: So , when we use cout , cin we are using basic_ostream , basic_istream objects ... ? And basic_istream and basic_ostream handle the facilities of o/p and i/p .. ???
ravi
@Mike: Well, I suppose you could make the standard streams have internal members which were extern, and the other things to simply have internal linkage, but what you say makes a lot more sense :)
Billy ONeal