Statements

The actual C++ program resides in the statements. Each statement can span multiple lines of texts. All statements are terminated by a semicolon character `;'. A sequence of statements within braces `{', `}' is a block. The scope of an instruction is the extent of the block it belongs to. Statements and block can be part of a declaration or a definition. Declarations specify a syntax, whereas definitions specify a semantics.

A declaration is used to create a new type or make the calling syntax of a function explicit. Examples:

class MyClass {
  char myChar;
  int myInt;
};
defines a new class called MyClass.
int myFunction(int myArgument);
defines calling syntax (also called prototype) of a function called myFunction, which is passed an int and returns an int. Declarations do not use memory and do not generate code; rather, they instruct the compiler on how handle variable symbols appearing subsequently in the code and how to issue function calls. Declaration blocks are terminated by a semicolon.

A definition specifies the meaning attached to a symbol. For a computer, the meaning of a symbol is given by what happens to the computer when the code relating to that particular symbol is executed. In other words, virtually all C++ code is part of a definition. For example:

MyClass* myPointer = &myObject;
is simultaneously a declaration and a definition. It is a declaration insofar as myPointer is a symbol being declared a pointer of type MyClass*. It is a definition because some memory of the correct size (i.e. enough to hold an address in memory, which on 32 bit architectures would be 4 bytes) is set aside and assigned to the symbol myPointer, and because that memory is filled with the address of memory where the object myObject is stored (see Fig. 2). As another example,
int myFunction(int myArgument) {
  int ret = myArgument * myArgument;
  return ret;
}
is a definition of the semantics of the function myFunction: it takes an integer argument, squares it, and returns it.

Leo Liberti 2008-01-12