Advertisement

different coding styles

Started by June 26, 2002 06:34 AM
10 comments, last by Overload 22 years, 4 months ago
Hi I''ve brought this book ("The complete idiot''s guide to C++) and have been learning C++ from it for about 6 months on and off. But then I find another book (SAMS Teach yourself C++ in 21 days) and buy it but even on the first few pages they are completely different. Heres the first program from the idiots guide: #include <iostream.h> main() { cout << "hello world" << endl; return(0); } And heres one from the SAMS book: #include <iostream> int main() { std::cout <<"Hello World\n"; return 0; } So I''m wondering which one is better. (I like the idiots guide one more because I''ve been learning from it for longer than the SAMS one )
The second one. iostream.h (along with fstream.h, strstream ...) gives you access to an older, non-standard, incompatible and deprecated stream library.

use the ''std::cout'' version :


  #include <iostream>int main(){  std::cout << "hello world" << std::endl;  return 0;}  


Or, if you get tired of explicit std:: scoping.


  #include <iostream>using namsespace std;int main(){  cout << "hello world" << endl;  return 0;}  


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
It is strictly equivalent!

Maybe the nicest is:

std::cout << "hello world" << endl;

Because you show the namespace of cout and mark the end of line with the correct inline stuff.
The SAMS book seems the better one (though I do remember it being on the list of terrible books to learn C++ from )

The iostream.h header is deprecated, you should ALWAYS use the iostream (without the .h) headers.

To avoid having to use the std:: name decoration, you can use the statement "using namespace std" at the top of your code. std is a namespace - look up some information on that if you want to know more.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Beaten to it


People might not remember what you said, or what you did, but they will always remember how you made them feel.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
So whats different about the new iostream opposed to the old iostream.h?
Advertisement
*BUMP*

Sorry I accidently bumped into the thread.

Imagine what I''ll do to a column!
I got a question. Why does it take up more room on your comp if you use rather than ? Its just some thing Ive noticed...
Among a number of issues...

iostream.h is char*-based; it''s interface isn''t standardised (different compilers may have different functionality). It is also a lot of trouble to handle. fstream.h vs. fstream is even worse...

iostream is std::string-based, which makes the binary larger, but the code is easier to write, maintain, and is portable.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Anonymous Poster
It is strictly equivalent!


I''m surprised no one here has yelled:

"NO IT''S NOT!" in response to this. Though the explainations of why are good.

quote: Original post by Anonymous Poster
...and mark the end of line with the correct inline stuff.


What the...??????!!

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/

This topic is closed to new replies.

Advertisement