The Basics
Welcome to the C++ tutorial!
Please follow all instructions on a source file. Ready?
Good! Now, to begin, we must make sure that we have every command we need. So, put
#include <iostream>
on the top part of your source file.
next, some commands you will use are in the standard "namespace", a storage device in C++. Put
using namespace std;
to use the items in the "std" namespace. Next, the modules of C and C++ are called "functions".
Most programs have at least one, called "int main()". Type
int main()
exactly as it is here.
Next, we need to show what part is in the function. to do so, add a curly bracket ('{'), right next to the 'p' key, like this.
{
This begins what is known as a "code block". Now, we want the function to show
something, like some words, right? Now, this next line is tricky, so I'll write it first, then explain.
cout << "Hello, world!" << endl;
"cout" is an 'object'. Think of the statement "Hello, world!" as going to this big building
called 'cout' for processing, and then showing up on the screen. 'endl' creates an endline,
like when you hit ENTER in Word. The next line is:
cin.get();
This waits for the user to press ENTER. There are other methods, such as
cin.ignore(std::numeric_limits<std::streamsize>::max, 'n' )
,
but that needs another #include statement at the beginning, and
system("pause")
, which is non-portable and creates security problems.
Finally, the "return statement", or
return 0;
This will end the program. On functions other than main(),
it will mean every time you use that functiom's name, it will be equal to
the return value. For instance, say I had a function like this:
1 2 3 4
|
int five()
{
return 5;
}
| |
That would mean every time I used "five()" in a program,
it would be equal to five. Now, finally, we need to end the function by putting
}
, and that's it!
Your source code should look like this:
1 2 3 4 5 6 7 8 9
|
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
cin.get();
return 0;
}
| |
I'll add many more pages, as it is to be a full-fledged tutorial.
Contributions are welcome.