Gw Temp


Tutorial - 'Comprehensive C++ Tutorial for Beginners - Part 1' by AzureFenrir

An item about Programming Languages posted on


A nice, long look at C++ and how to get started with it.


Comprehensive C++ Tutorial for Beginners - Part 1

Hi there. If you do not know me (you probably do not), my nickname is AzureFenrir, and this tutorial is *supposed* to teach you the basics of C++. Just promise not to sue me if it doesn't accomplish its purpose, capiche ;)?

So you want to learn to program C++? Great! C++ is a powerful language that can be used to make anything from basic to really advanced multimedia programs. But...before you can use such a great tool, you have to know how to use it!

*Insert reader groans here*

Hey, didn't I tell you not to call your lawyer? You didn't? Good boy! That's one less lawsuit for me today :).

Anyway, the basic C++ syntax is still best illustrated by the *trumpet roll* HELLO WORLD! program. As banal and cliché ¡s it sounds, HELLO WORLD is relatively easy to dissect and probably easy for a first-time programmer to grasp.

So, open your C++ compiler, and type in the following phrase:

#include <iostream.h>

What does this do? It adds the "bells, whistles, and other stuff" that you need to type stuff into a DOS window and accept more stuff from the user.

Now, type in the following after the #include line:

int main()
cout << "Hi ATARI!";

return 0;

That's your basic Hello World program :). Don't worry, I'll explain the various parts of the program.

Every C++ program has to have a main function. When you make programs that flash stuff in DOS windows, this main function, or "subprogram," or code subdivision, is conveniently named "main." The line:

int main()

Basically tells the compiler that you are defining a function called main. If you do not understand the concept of functions yet, don't worry. I'll explain that in a later tutorial. For now, just realize that int main() is the code that yells to DOS, "HEY!!! LOOK AT ME!!! I'M MAIN!!! I'M ALL SPECIAL AND 1337 AND WHATNOT SO RUN MY CODE FIRST!!!"

Now there's a left brace ({), followed by more code, followed by a right brace (}). So what the heck are those ugly looking things doing there?

Well, the braces { } enclose a "block", or group, of code. The two braces basically group the statements:

cout << "Hi ATARI!\n";


return 0;

together into one block of code. This tells main(), "Oh! Everything inside the braces belongs to me! I'm special and I have all two lines of code!!! Yay!" If you omitted the brackets, like this:

int main()
cout << "Hi ATARI!\n";

return 0;

Then main() will think, "Oh, so I only have one line of code. How sad for me...but wait! There's another line, return 0;, that's sitting out there, and it's not claimed by anyone else! That's an environmental waste!!! I will throw a flaming error and refuse to compile! Yeah, that should teach him not to litter code!"

(VOCABULARY: compile: to change a piece of hand-typed code (like C++ code) into an executable program)

Anyway, the cout means Console Output, or "write stuff on that ugly black DOS window." cout << "Hi ATARI\n"; tells DOS to write "Hi ATARI" followed by a LINE FEED (basically start a new line). Yes, \n means "Start a new line."

The text that you want to write must be enclosed by double quotes ("). So, if you want to write:

Bart was, is, and always will be my king![ENTER]

You would use:

cout << "Bart was, is, and always will be my king!\n";

Did you notice that semicolon on the end? Every actual line of code must end with the semicolon. The semicolon basically tells the compiler that one line of code has ended and another line is about to begin. However, you DO NOT add a semicolon after the int main(). Why? If you did, it would mean that the main function ENDED at the semicolon. But wait! If main ended there, then it couldn't "possess" the block of code after it, which would mean that the whole block of code was an environmental waste! Of course, you know how much C++ compilers hate littering code. You do not include a semicolon after ({) and (}) either.

Finally, we have the code:

return 0;

This tells the compiler that the function (and in this case, the program) has ended with no errors.

Now that you know how to write stuff on that ugly DOS window, its time to learn how to ask the user for stuff. As you know, a program would not be very useful if it just displayed text continuously...or in computer nerd terms, "All Output and no Input." Thus, if you want to be a good C++ programmer, you MUST learn how to obtain input from the user.

[AzureFenrir continues in a monotonous voice for hours. Suddenly, a masked GW member barges into the room and bombards AzureFenrir with GW Productions? Multi-Purpose Tar and Feathers. Two days and several baths later, a slightly wet AzureFenrir returns unharmed and continues to write this tutorial]

Anyway, to accept user INPUT, you must use another statement, cin, which stands for Console Input. It's used like this:

cin >> variable;

But wait! What the heck is a variable?! If you've ever used a game maker (like RM2K), then you have an idea of what variables are. If not, variables basically "contain" a value that you can modify and retrieve. You an create a variable like this:

int variablename = 0;

Variablename is the name of the variable. It can be ANYTHING, as long as it contains only letters, numbers, and underscores. Furthermore, variable names cannot begin with a number. It must start with a letter or underscore.

int tells us that the variable contains an integer, or a whole number. If you want the variable to store something else, you can substitute int for something else, like:

Boolean value. Can only contain 0 and 1. Acts similar to RM2K switches.

Can contain ONE ASCII character, like 'a', 'G', '6', or '&'.

Can contain a whole number.

Can contain a whole number. Can contain numbers too big for int to handle.

Can contain a decimal number, like 5.324.

Can contain a decimal number. More precise than single.

The zero at the end basically tells the compiler that the variable starts off with the value zero. If this is omitted, then the variable will start off with a really small number (like -2^64).

So, what are variables used for? They are most useful for input and for storing numbers/letters for future use. They an also be manipulated, like:

int variable1 = 0;
int variable2 = 0;

variable1 = 5;
variable1 = 4 + 7;
variable2 = variable1 + 3;

As you can see, performing arithmetic operations with C++ works the same way as real-world arithmetic operations. Therefore, if you want to calculate five plus five, then you can simply use 5 + 5. Similarly, five plus the variable x can be expressed as 5 + x. The symbols used for multiplication and division are (*) and (/), respectively. Prentices ( ) may also be used to group operations.

So, back to user input. If you want to ask the user, say, how old is ATARI, you can use something like:

int ATARIAge;

cout << "What is ATARI's age?\n";
cin >> ATARIAge;

Then, when you run the program, you'll see something like:

What is ATARI's age?

(The _ represents the input cursor)

Now, you can also display variables, like:

cout << "X equals " << x << "\n";

Notice how I used three << signs? That is perfectly legal in C++, so if you want to display two, three, or many different things, you can type:

cout << thing1 << thing2 << ... << thingn;

So, let's modify our Hi ATARI program to include some user input:

#include <iostream.h>

int main()
int ATARIAge = 0;

cout << "Hi ATARI!\n";
cout << "What is ATARI's age?\n";
cin >> ATARIAge;
cout << "ATARI's age is " << ATARIAge << "!\n";

return 0;

If you run this program, you should get:

What is ATARI's age?

If you typed in an age, you'll get something like:

What is ATARI's age?
ATARI's age is 13!

How cool is that? (No offense meant, Mr. ATARI. I just needed an example.) this tutorial, you should have learned the following:

That's all for now :). In the next tutorial (that's right, there's ANOTHER one), I will discuss more features of C++, including IO control, more arithmetic operators, as well as introduce the concept of boolean operations.

*Insert reader groans/lawsuits/tar and feather here*

Hey! Why are you groaning? You should have known that this is only the first of a ELEPHANTINE CHAIN of C++ tutorials covering everything from DOS-based C++ to conceptual programming to Visual C++ to DDE/OLE to...hopefully, COM+.

I must now disappear before they find me. So...look for my next tutorial and have a nice da----

[AzureFenrir is abducted by a group of GW members with black masks]