Archived

This topic is now archived and is closed to further replies.

different coding styles

This topic is 5652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 )

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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 */
/*=========================================*/

Share this post


Link to post
Share on other sites
A lot of books where written before C++ was standardized. So you need to make sure any book you read follows the final ANSI standard.

Namespaces are good. Mostly you can ignore them (using namespace std, but if there is a conflict you can always fall back on explicitly qualifiyng everything. (std::endl).

FYI Depreciated means that an obsolete feature still works (backward compatiblity), but you should stop using it because it will evenutally be removed from future versions of the product.


Share this post


Link to post
Share on other sites