Here is a first example of a C++ program, one which virtually every C++ programmer learns when starting off:

// So we can use the "cout" command #include <iostream.h> void main() { cout << "Hello World!\n"; /* this is pretty simple */ }

This program does almost nothing useful except that it gives a new programmer some sense of how a program might be organized. If you compile and run this program, the computer prints out "Hello World!" on your screen, and then halts. Let's look at the parts of this program:

The first line lets the programmer use extra functions, such as cout. You must #include header files (or ".h" files) like these at the start of your program in order to use the functions/variables/classes that they define. The syntax is:

#include <header_file_name>
(Note that some header file names end in .h and others don't).

You can also easily create your own header files for use in your programs if you want to be able to reuse your code or want to better organize your programs.

Above the #include line is a comment. C++ will completely ignore anything you write after two forward slashes (//) until the end of the line. Another way to comment, which you can find a few lines later, is the standard C comment syntax. Simply type any message you want between the start-comment marker (a forward slash followed by and asterisk, /*) and the end-comment marker (the opposite, */). The message can span many lines as needed, as shown in the typical commenting style below:

/* * The C++ compiler can not see ANYthing I type here * because I typed the symbol /* However, it can see my * code again after I type the next line. */

The third line of the program is blank, which is perfectly legal. You can insert as many extra blank lines or "white space" as you'd like in order to make your program more (or less) readable. Spaces and tabs are also considered white space.

The third line is one which appears in every program. It introduces the main() function, which is where your programs will always begin to execute instructions. The main() function is often of return type void. More on this topic later. For now, just accept that every program needs to have a main() function to tell it where to start, and it should appear after your #include statements.

After any function definition (more on functions later), there must appear first an open brace ({~~), then all the code for the function, and finally a closing brace (~~}) to indicate the end of the function definition. The executed part of the program is what's between the braces. In this instance, the one line in main() function tells the computer to print "Hello World!" on your screen.

Now that you have some idea of a very simple C++ program structure, here is a more general program structure:

1) Comments-- It is conventional to including a few details about your program at the top of the file, both for documentation and so that other programmers can better understand your program. You will likely want to write the name of the file or program, the version, the date, your name, your company or school, and a quick synopsis of the program's functionality.

2) Header files-- As mentioned above, #include other files containing function definitions that you will need.

3) Class definitions-- Classes let you create objects. They are essentially new data types that may contain functions for retrieving, setting or manipulating the objects to which they belong. This will set up much of the functionality for the rest of your program.

4) Member function definitions-- Classes usually contain functions, most of which will be defined outside of the actual class definition. Member functions are defined outside of the class definition brackets not only to keep your program better organized, but also because functions defined within the brackets are inline functions by default, which you may want to avoid (or at least let the compiler make decisions about).

5) Other function declarations -- Functions not particular to any class and not #included in a header file are declared next. You may also define them at this point, but most functions are simply declared; that is, without coding the body of the function. Such functions are known as "global" functions because, like global variables, they are visible everywhere in the program.

6) Global variable declarations-- Variables that will be visible to all parts of your program should be declared before main().

7) main()-- As in the "Hello World" example, main() must appear in every program. This is where it all starts.

8) Function definitions-- It is your choice whether or not to define your functions when you declare them. Usually, functions longer than 1 or 2 lines are defined below main() for easier readability.