Absolute Beginners C++


Lesson 1 - Essentials

1.1 Structure of a C++ program

  
// ex1-1.cpp
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello";
    return(0);
}
ex1-1
Hello
Example ex1-1.cpp shows the basic elements of a C++ program. The source file is made up of lines of plain text which contain two types of instructions: those which are instructions to the C++ compiler itself, and those that make up the program which you require the computer to execute on your behalf. The first line in the sample is an example of a 'comment', a line of text which is ignored by the compiler. The comment is there for your own benefit. In general any text that occurs between the symbol '//' and the end of the line is ignored.

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

  
// ex1-2.cpp
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello";
    cout << " ";
    cout << "there.";
    cout << endl;
    return(0);
}
ex1-2
Hello there
In example ex1-2.cpp, you can see that additional output instructions have been added to the program. Each new instruction is separated from the last by a semi-colon character. You will see that each instruction is also on a new line and indented by 4 characters, but this is just a convention and is not a required format. In fact the compiler ignores sequences of more than one white space character, except inside double quotes. 

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:

  
// ex1-3.cpp
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello" << " " << "there." << endl;
    return(0);
}
ex1-3
Hello there
In example ex1-3.cpp the four different things that we wanted to print in the last example are still included, but here we show that we can put them all into one instruction. The 'cout' object can accept a sequence of output operations, each with its own argument.

1.4 Exercises

a. Write a program (hellocpu.cpp) to print "Hello <name>", where <name> is your name; as in: 
hellocpu
Hello Mark
b. Write a program (initial.cpp) to print a large version of the initial letter of your name in 'X' characters; as in: 
initial
XX     XX
XXX   XXX
XXXX XXXX
XX XXX XX
XX  X  XX
XX     XX
XX     XX

© 1999 Mark Huckvale University College London