C++ tutorial 1 - Basics


What you need:
Ok, so once you go get those, let's talk about what good C++ is for.

Why would I want to learn C++?
C++ is a powerful, extensive programming language. I say extensible because there is a wide array of "libraries", or basically extensions, to the language. Using these, you can create games, GUIs (Graphical User Interfaces, ie Windows), art, web pages -- the possibilities are almost endless. You might use C++ for work, to sort things, to do things with money (you know what I'm talking about, hopefully), or just for fun, as I do.
What are some examples of things I can do with C++?
You will see some examples of C++ applications in this tutorial, as well as on the Projects page.

Hello World
Here is the classic "Hello World" program in C++ (In this tutorial, as well as in all the other tutorials, the program starts at the green Code):

Code

#include <iostream.h>

int main(int argc, char** argv)
{
    cout << "Hello, world!" << endl;
    return 0;
}

End Code

Well, if that wasn't cryptic! Don't worry -- If you feel this way right now, that's normal. You're still learning, and by the end of this tutorial, you'll be able to write that with your eyes closed. Let's go through it, line by line (the piece of code (program text) will be in green.)..
Hello World, explained
#include <iostream.h> -Includes the library called "iostream.h"; Remember that libraries are like little extensions to the program. We'll talk more about them later.
int main(int argc, char** argv) -The function called main, taking the parameters argc of type int, and argv of type char**. Once again, functions will be explained later.
{ - Beginning of a block.
cout << "Hello, world!" << endl; - Calls the function cout, taking parameters "Hello, world" and endl.
return 0; - Makes the function main return the value 0. return Will be explained in the functions section.
} - End of a block.

Now that we've broken it up, the next few sections will introduce you to the terms introduced above. We'll start with statements.
Statements
In C++, a statement is basically a command. For example, if you wanted someone named Gregor to open a door in real life, you would simply state:

"Please open that door, Gregor."

And, if Gregor is a nice guy, he would rush off to open the door. Let's notice the basic structure of this simple statement: It ends with a period. It has verbs, like 'open'. It has other punctuation, like the comma in the middle. And, it has nouns, like 'door' and 'Gregor'.
C++ statements are very similar to the English statement above. They end with a particular character. They have verbs and Nouns. And, in some cases, they have special punctuation in the middle. I'll give you a few simple C++ statements, so you can try to see what they do, and compare them with English statements. As always, they will be in green.

b = 6;
open_door();
a = 5 + 5;
Gregor.open_that_door();
Gregor.walk_around(the_room);

Are you starting to see what I mean? If you say most of these out loud, they will sound like normal English sentences! Hopefully, you have noticed the key points I tried to point out. If not, take a good look at all of the statements. You should have noticed (and it's okay if you still haven't, everyone learns at a different rate):
  • Most importantly, all C++ statements end with a semicolon (;). This is true in all of the statements above, and all the statements there ever will be.
  • Second most importantly, all verbs in C++ end with () or (something). We'll discuss why in a later section.
  • A certain noun, Gregor, performed verbs by having his name called, followed by a period, and then the verb's name, with correct punctuation as stated above. As you may know, this is not the only way to do verbs. If you're having trouble understanding, this is our example of "special punctuation", like the English comma. If you were to pronounce the fifth example, you would say, "Gregor, walk around the room." And, if you ran that statement (but don't!), Gregor would happily walk_around the_room until you told him to stop.
So, by now, you should have a basic understanding of how C++ statements work. You can review by looking back through the section, and then test yourself with the following quiz. Good luck!



Statements -- Quiz

Ok, now that you're ready, I'll give you the three-part quiz on C++ statements. In the first part, you will have to give a short answer to the question presented. I  the second part, you will have to write out, in plain English, what the presented statement would translate to. In the third part, I will give you a plain English statement, and you will have to write it in C++. For all of the questions, you will write the answers on your own sheet of paper, not the website, your computer screen, your cat, but a sheet of paper. Once again, good luck!

Part one -- short answer

    1. What does every C++ statement end with?

    2. What does every C++ verb end with? (There are two answers -- I want both!)

    3. How does a noun in C++ do a verb?

Part two -- C++ --> English

    1. run(Bob);

    2. Sally.eat_bananas();

    3. Gregor.decorate(the_christmas_tree);

    4. sally_likes_bananas = true; (Hint: Think of '=' as 'is')

Part three -- English --> C++

    1. Get the coke out of the freezer.

    2. The doctor's instructons are to take one pill. (Hint: Think of 'are' as '=')

    3. Sally likes bananas.



Answers on next page.