![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Road to C++
Hello there.
I have purchased the book: SAMS Teach Yourself C++ in 21 Days, after my hate towards purchasing the C++ book for dummies (HEAP OF TRIPE) I have gotten through Lesson 1 (first day) so far and i have a few questions i would like to ask to help me understand it better. This is the first program you write (obviously): #include <iostream> //what does this do? I'm guessing the book will explain this later one, but i want to know now! lol Unless if it is best not to yet?
int main() //As previous comment
{
std::cout << "Hello World!\n"; //Whats the benifit of using std:: instead of declaring "using namespace std;"
system ("PAUSE"); //I had to add this so that I coukld see the output.
return 0; //what does this do?
}My questions are in the comments Also have question on this #include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl; \\ dev C++ came up with an error with this and std::cout << end; so i removed them, what are they for?
std::cout << x + y << " " << x * y;
std::cout << end; \\ dev C++ came up with an error with this and std::cout << end; so i removed them, what are they for?
system ("PAUSE");
return 0;
}Also, is my purchase of this book a good choice? (I understand it won't make me an advanced C++ programmer, but it will get me started says its aimed at levels begginer to intermediate, so i should be intermediate after, with lots of practice) |
|
|
|
|
|
#2 | |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
Quote:
std::cout << std::endl; std::endl = new line. Same as the escape sequence \n, if you have seen that one. |
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Quote:
Thanks, i guessed it was the same as \n |
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
It appears that you really should read a bit more of the book. I have that book, just haven't read it yet, so I can't comment on how good it is. But if #include isn't explained within the first chapter or two you may want to consider another book. Your first two questioned lines should be covered quite well in the early chapters of the book.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4
![]() |
Yes, read more of the book.
Do this #include <iostream> using namespace std; std:: |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Instructing the OP to use "using namespace std;" as a matter of course is also incorrect. The desirability of doing that is entirely circumstantial; relating to the coders use (and degree of use) of his/her own (possibly overlapping) namespaces. In simple, short programs, it's usually to be preferred, but it shouldn't be presented as a "do this instead of that."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4
![]() |
My mistake if it sounded like a rant.
I meant to say it as a recommendation. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Others have answered some of the questions. I'll answer some more;
#include <iostream> is a preprocessor directive, which literally pastes the contents of something (commonly called a header file, as it might actually be a text file containing declarations and definitions that are valid C++) named iostream in place of the #include directive. The thing called iostream is a standard header, described in the C++ standard, that allows use of basic stream I/O functionality. For example, std::cout is an output stream, corresponding to a device known as "standard output" that is declared in <iostream>. int main() is an entry point to a program. When any program starts up, it is necessary to have some way of reaching code that the programmer wants executed. main() is referred to an an entry point and is (typically) the first function under programmer control to be called. There are three forms of main() that all C and C++ compilers must support: int main() -- which takes no arguments is one of them. All required forms of main() return int (which is normally interpreted by your operating environment as an error code). The benefit of using "std::cout" instead of "using namespace std;" and simplifying it to "cout" is one of avoiding ambiguity (which typically causes a compiler to choke and refuse to compile a program). The std:: prefix means that the object named cout exists in a namespace (literally a universe of names) named std. It is possible to have an object named cout exist in another namespace, in which case using directives can result in ambiguity. In your example, it makes little difference. But here is an example where it does; #include <iostream>
namespace X
{
int cout;
}
using namespace std;
using namespace X;
int main()
{
cout << "Hello\n";
} |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
grumpy has explained your issues, and as to whether or not it's a good book, i think it is. it goes from hello world to fairly complex things such as multiple inheritance and polymorphism with good explanations. 21 days is a lie though, the book should probably take you 2-3 months to finish (and understand everything).
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4
![]() |
The time to understand the book depends on your programming experience. If you have not programmed before it will take you longer to understand it good.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|