Currently in the process of learning C++, I picked up a free ebook titled "How to Think Like a Computer Scientist, C++ Edition." In the book, the "Hello world" program is given as:
#include
Firstly, with a modern C++ compiler, you shouldn't use 'void' with main. Instead, use int; must be a terrible book if it uses void main() everywhere O.o: int main() { // program return 0; } nowadays, you would only need to write #include <iostream> instead of the old-style: #include <iostream.h> also, you need to prefix both cout and endl by std:: , so the program looks like std::cout << "Hello, world." << std::endl; or you can add the line using namespace std; so you don't have to prefix cout and endl by std:: A working program looks like #include <iostream> using namespace std; int main() { cout << "Hello, world." << endl; return 0; }
why is it all different from the book? About using int main() over void main(): every C++ program must pass a return code of type int back to the operating system, which is why main must always be declared int and should include a `return' statement somewhere in the main function. Actually, you can ignore having to include `return 0' since the compiler will automatically do it for you anyway, as long as you declare main as returning int (and not void). about prefixing everything with std::, or having to use `using namespace std;' C++ has something called namespaces, and if you wish to use anything that comes from the C++ standard library (things like cout and endl), you have to prefix them with std::, or have the line `using namespace std;' in order to let the compiler know that these objects are from the C++ standard library.
Wow... Well, so far the book uses void for main() in all the examples I've found. o.O Perhaps I should go with a more up-to-date book. At least I have the C++ pocket reference from O'Reilly to help, though.
Thanks for the clarification! :)
I learned some rudimentary C++ through Bucky Robert's video tutorials: http://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLC6E50B89DA30C77A&index=1&feature=plpp_video For textbooks, I would check out some of the texts listed here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Excellent! I will check those out, indeed. :D
I love Bucky. He was a great help when learning Java.
Join our real-time social learning platform and learn together with your friends!