relationship between std and iostream

This is a discussion on relationship between std and iostream within the C++ Programming forums, part of the General Programming Boards category; this is code: Code: #include <iostream> using namespace std; int main() { cout<<"Hello world"; return 0; } here i must ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    relationship between std and iostream

    this is code:
    Code:
    #include <iostream>
     using namespace std;
    int main()
    {   
    	cout<<"Hello world";
    	return 0;
    }
    here i must write both #include <iostream> and using namespace std; i don't understand why such a expression we need.Where is cout?Is it in iostream or std?

    Also said that iostream library is part of std.So why do we write include file.Also if std cover iostream why don't we write first
    using namespace std;
    then #include <iostream>
    I don't understand relationship between two expression.
    Any help will be greatly appreciated
    Last edited by sawer; 01-24-2006 at 09:23 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    foo.h
    Code:
    #ifndef FOO_H
    #define FOO_H
    
    namespace foo
    {
        int bar;
    }
    
    #endif
    foo.cpp
    Code:
    #include "foo.h"
    using foo::bar;
    int main()
    {
        bar = 5;
        return 0;
    }
    As an example, the integer variable bar is declared within the foo namespace in the foo.h header file which is #included in our foo.cpp source file. Understand this and then apply it to the std namespace/iostream header problem you are having.

    The headers are usually needed to provide the basic declaration of objects and their interface (the member functions) and data members and their types. A lot of this is done for type-checking purposes by the compiler. The actual code for the functions are typically contained within "libraries".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok.thanks.
    Can we say every standart library include namespace std so std covers every std library.I mean
    string.h
    [code]
    namespace std
    {
    //memebers
    }
    [/]code
    or
    iostream.h
    Code:
    namespace foo
    {
       //members.
    }
    like these...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Basically yes, every C++ standard header has its contents placed within the std namespace.

    However, string.h is a standard C header that is standard in C++ but has a preferred C++ version: <cstring>. So <string.h> does not have its contents in the std namespace, but its C++ counterpart <cstring> does.

    Also, iostream.h is not standard at all. So if you have an old compiler that supports <iostream.h>, then it will not have its contents in the std namespace. However, the new, correct and standard version, <iostream>, does have its contents in the std namespace.

    So just replace the old headers in your example with the new, standard C++ headers and it will be correct.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok.thank you.
    I understand what you mean.

    Good works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why can't I overload of iostream operator?
    By Mathsniper in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 09:20 AM
  2. namespace std
    By blankstare77 in forum C++ Programming
    Replies: 27
    Last Post: 07-24-2005, 01:33 PM
  3. iostream.h (vs) iostream
    By Brain Cell in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2004, 08:20 AM
  4. iostream question
    By xddxogm3 in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2003, 04:48 AM
  5. iostream problem. Help! :P
    By skyvoyager in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2002, 11:14 PM

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