Program Structure

The best way to start learning a programming language is by writing a program, which is exactly what we did in the page "Getting started with Dev-C++ ". The code we wrote, once compiled, produced a program that simply printed a message on the screen. Here is the code once more:

// A first C++ program

#include <iostream>
using namespace std;

int main ()
{
  string s;

  cout << "Greetings, Maestro!";
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}

This program simply prints the message "Greetings, Maestro!" on the screen, but it includes the basic elements found in all C++ programs. Let's take a look at each of those elements in turn, and see what they actually do.



Comments

// A first C++ program

This is a comment line, and is used to convey information about the program to someone reading the code. All lines beginning with two forward slashes (//) are comments, and have no effect whatsoever on the execution of the program. Comments are a convenient method of providing documentation within the program itself. The form of comment shown above is often used if the comment will only occupy one line in the program. Note that block comments can also be included in a program and should be enclosed using a combination of the forward slash and asterisk characters as shown below:

/* This is a block comment.
It can occupy several lines */

We can even create a rather elaborately framed comment, as shown below. You may feel that this kind of thing is totally unnecessary, and that the programmer has too much time on his or her hands. On the other hand, you could argue that it draws attention to an important comment that might otherwise be overlooked. How to write comments is to some extent a matter of personal choice - institutional constraints and conventions notwithstanding, of course.

/***********************************
* This is a somewhat more          *
* contrived block comment.         *
* It boxes the text up nicely,     *
* but could conceivably be seen as *
* being somewhat superfluous.      *
***********************************/



Preprocessor directives

#include <iostream>

Lines beginning with the hash sign (#) are called preprocessor directives, and are used to provide information to the compiler. In this case the preprocessor directive instructs the compiler to include the iostream library header file. This file includes declarations for functions that will be used by the program.



Identifying a namespace

using namespace std;

The elements included in the standard C++ library are declared within a namespace called std (a namespace, in the context of a programming language, is essentially a list of keywords used to identify programming elements). The above statement tells the compiler that we will be using programming elements defined within this namespace.



The main() function

int main ()

This line declares the program's main() function, and indicates the point at which program execution actually begins. All C and C++ programs must have a main() function. Other functions outside of the main() function, whether they appear before or after it, are called from within main().

The keyword int appearing before the word main indicates that the program will return an integer value to whatever process called it. This value can be used to indicate the exit status of the program (e.g. success or failure).

The parentheses following the word main are required because this is a function declaration. The parentheses in any function declaration may be used to enclose any parameters (or arguments) that may be passed to the function. Even if a function does not require any parameters, the parentheses must always be included. The code that constitutes the body of the function is enclosed in curly braces({}).



Program statements

string s;

This program statement declares a variable called s, of type string. A C++ program statement is a high level command that causes one or more low level machine code instructions to be executed by the computer's processor. Notice that statements are always terminated with a semicolon.

cout << "Greetings, Maestro!";

This statement causes the text "Greetings, Maestro!" to be sent to the standard output stream (represented by cout), usually the screen). The cout stream is defined in the iostream library header file within the std namespace, which is why it needed to specifically referenced by the #include <iostream> pre-processor directive.

cout << "\n\nPress ENTER to continue.";

This statement outputs the text "Press ENTER to continue." to the screen. Note the use of the escape characters ("\n\n") which output two newline characters, each of which moves the cursor down the screen by one line and sets it at the leftmost position.

getline( cin, s );

This statement calls the function getline(), which is defined in the string header file. The getline() function gets a line of text from the user. The argument cin is used to identify the standard input stream (usually the keyboard), and the argument s indicates the name of the string variable where the string data will be stored.

return 0;

The return statement terminates execution of the main() function, and may be followed by a return code (0 in the example given above). A return code of 0 is generally used to indicate to the calling process that the program worked as expected (i.e. it did not encounter any errors during its execution).

Note that each program statement appears on its own line, and that the statements that appear within the main function are indented. This is not necessary for the program to be correctly compiled, but it makes the program more readable. The use of indentation, and placing separate program statements on different lines, are considered to be good programming practice.


The output of your first C++ program

The output of your first C++ program