Lesson 1 - Essentials1.1 Structure of a C++ program
The second line is an instruction to the compiler to incorporate into this program some standard software components for performing Input and Output on streams of data. The part of the instruction '#include <>' tells the compiler an inclusion is required, while the string 'iostream' indicates which component is required. The third line is an instruction to the compiler that this program will be referring to some standard objects which can be found in an area called 'std' - the standard namespace. The object 'cout' is the only standard object in this example. The fourth line provides a name and a type to the single block of code in our example. We have chosen to call the block of code 'main' and have required it to return an integer (whole number) value to the operating system after it completes running. For console applications on Windows platforms, every program needs to have a block of code called 'main' and this needs to return an integer; so we didn't really have much choice. When the program is run, the 'main' block is the 'entry point' for execution: where the program starts running from. The fifth line and last line define the sequence of instructions that make up the 'main' block. Opening and closing 'curly bracket' symbols are used to define a block. Note that each instruction inside a block ends in a ';' semi-colon character. The sixth line is a request made to a standard object called 'cout'. This object 'stands for' the output channel to the console (or screen). The request we make of it is to print a sequence of characters. We do this by enclosing the sequence (called a string) of characters in double quote marks and putting the string after the operation '<<'. Thus the object 'cout' gets told to output something (by '<<') and the something is the string enclosed in "...". To print a double quote mark by the way, you can prefix the double quote mark with a back-slash, as in "this is a \" quote mark". Finally the seventh line causes execution to 'return' to the component that called this block. In this case, it was the operating system, so the thread of execution returns to the operating system. The 'return' instruction provides a value with which this block of code returns. We have already said that the block must return an integer, and in this example we decide to return the integer 0. If you run this program inside the SCITE editor, with the Tools/Go command, the returning value ('Exit Code') is printed on the screen. |
||
1.2 Multiple Statements
There is one other new component in this program: the 'endl' object is simply a character that 'stands for' the end-of-line character. When an end-of-line character is sent to the cout object, the computer starts printing at the beginning of a new line on the screen. |
||
1.3 Output a stream of values:
|
||
1.4 Exercisesa. Write a program (hellocpu.cpp) to print "Hello <name>", where <name> is your name; as in:hellocpu Hello Mark initial XX XX XXX XXX XXXX XXXX XX XXX XX XX X XX XX XX XX XX |
© 1999 Mark Huckvale University College London