Colour Theme Font Size Options
 
 
 
Basic Structure

C++ Program Structure

There are two basic parts to a C++ program:

  1. Instructions to the C++ preprocessor and compiler.
  2. Instructions that describe the processing to be done.

However, before we can describe these instructions, we must have a way of naming things so that we can tell the compiler about them and describe what we want to have done to them. We name things (data types, data objects, and actions) by giving them an identifier. An identifier is made up of letters, numbers, and underscores, but must begin with a letter or an underscore. We use the words identifier and name interchangeably.

Beware: C++ is case sensitive. That means that Value, VALUE, value, and vaLue could be four different identifiers.

C++ Program Structure

Let's examine the following C++ program. We have numbered the lines so that we can discuss them easily.

  • Line 1-9 make a comment block that gives a brief introduction to the C++ program.
    By convention, every single C++ program should have this kind of comment block at the beginning of the program.
    Usually, a comment that carries over several lines begins with /* and ends with */
    A comment on just one line - or part of the line - begins with //
    Typically, you will see an inline comment at the beginning or end of a processing block such as // end of program.
  • Line 11 and 12 tell the compiler to add console input and output (iostream) features to the program and to make them easier to use by adding their namespace into our program. You will probably use these in every program this semester.
    • Line 11 is what's known as a preprocessor command. The pound(#) sign followed by include begins every preprocessor command.
      Any built-in or user-defined header files you wish to use in your program must follow this same syntax.
      Note: Built-in header files are enclosed inside the '<' and '>' marks. Header files you create and bring in to the program are enclosed in "" quotation marks.
  • Line 14 int main() is the executable part of our program. The function name is main, and it returns a value that is of type integer. The function has no parameters, as indicated by the empty parentheses.
  • Line 15 and line 21, the curly braces { } mark the beginning and end of a group of C++ statements. In this case, they enclose our main function.
  • Line 16, int number; sets "number" as a variable of type integer.
  • Line 17, cout is the command to print text or data on the screen. Anything that is enclosed in quotation marks is to be printed.
  • Line 18, cin - command that receives data from the end user.
    ** The <iostream> file exists in every C++ program, and it is this file which allows us to use cout and cin. **
  • Line 19, the second cout line prints out "You entered: [number]"
    so, if I entered 5, the output would be: You entered 5
    This kind of way to get data into the program is called a user interactive input, and it makes the program become an interactive program.
    endl - A manipulator to indicate the end of the line. It tells the cursor to stop printing on the current line, and begin on the next line.
  • Line 20, return 0; - this line is there, since the return value in our main program was designated as an integer. Just prevents the compiler from reporting a syntax error.
  • Line 22, // end program - a single line comment.

Semicolons terminate statements in the C++ program. There are 6 semicolons in the preceding program, so there are 6 statements in the program.