Synfire's Guide to C++

1. Basic Structure

C++ like all languages have a structure that you use to create your programs. C/C++ both use the:

  'define'->'initiate'->'main()'->'functions'

type setup. As you move along you will see your programs evolve into such a structure.

simple.cpp
#include <iostream.h>

int main()
{
    return 0;
}

The example code above is really useless, but it will compile and run. This will allow me to show you how a program reads.

First things first, on line 1 you see '#include <iostream.h>', this tells the compiler to add all the code from the iostream.h header file into your program just as though you had typed it yourself. You will always use header files, just different ones depending on your needs at the time.

On line 3 you see the beginning of your program. main() is a special function that is automatically called when your program executes. Notice the 'int' before main(). This tells us that the main function will be returning a small numerical value.

Lines 4-5 contain the BLOCK, a block is a chunk of codes wrapped in curly brackets. Within this block are the commands that your program will execute. In our example the 'return 0;' command just ends the program with a return of 0. I am not a stickler on being a proper programmer, therefore, I usually save that line by changing 'int main()' to 'void main()' so that I don't have to return a value.

When you create your programs you might wanna keep notes that can help you remember things when you come back to the program. This is known as setting comments, there are two types of comments that C++ will recognize, single line and multi line. A comment is over looked by the compiler and it is good practice, and just plain smart, to use comments to make programming over along period of time. Here is how to do both:
// This is a single line comment anything on this like
// or this line is overlooked. The // ensures that this line is
// overlooked.

/*
This is a multiline comment, I can write as 
many lines without using // to censor each line
the /* symbol starts a multiline and the ending
symbol is:
*/
Summary
In this section you have learned how a C++ program should look and how to add comments to your program.

< < < Introduction | Lesson 2: cout and cin > > >