Getting Started with C++

In order to get started with creating programs in C++, software is needed to facilitate the writing and compiling of a program. Writing a program in C++ can be done in a simple text editor, like Notepad, however, using an IDE, or Integrated Development Environment, can have many benefits. An IDE not only provides a means to write a program, it may also include features such as code completion, where it offers suggestions to complete a line of code, based on what has been typed so far. This is particularly useful if you are new to a programming language or, starting programming for the first time.

In order for a program in C++ to run, it must first be compiled. This is where a program is converted into a form that a computer can read. Some IDEs incorporate a compiler so that one does not have to be installed separately. Code::Blocks is an example of an IDE that provides a means to write, compile and run a program written in the C++ programming language.

To aid in the writing of a program, many IDEs, including Code::Blocks, colour code various aspects of the program to make it more readable, as well as easier to maintain and troubleshoot. Below is an example of a basic C++ program that displays the message ‘Hello world’ out to the console.

#include <iostream>

/* Basic program to display a message
   in the console. */
int main() 
{
    // Display a message in the console.
    std::cout << "Hello World" << std::endl;
    
    // Terminate the program.
    return 0;
}

The functionality provided in C++ is split up into what are known as header files. When a particular piece of functionality is required in a program the relevant header file needs to be incorporated using the ‘#include’ statement. This is discussed in more detail in the next section.

The text in green above are comments. When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. The first comment above is a multi-line comment. Multi-line comments must start with ‘/*’ and end with ‘*/’. For single line comments, such as the second example above, the comment must be preceded by ‘//’.

The words in blue are known as reserved words, or keywords, which have a specific purpose in the language and can’t be used for anything else.

All programs written in C++ have a function called ‘main’, where execution of the program begins. The code within the ‘main’ function is encompassed with opening and closing curly brackets, or curly braces, as they are sometimes called.

In this simple program, ‘std::cout’ is used to display the message, ‘Hello World’, out to the console. The ‘std’ part of this refers to the C++ standard namespace called ‘std’, which ‘cout’ is a part of. A namespace is a name given to a portion of code to help reduce the risk of naming conflicts. By stating ‘std’ prior to ‘cout’ it qualifies that ‘cout’ should be used from the ‘std’ namespace.  At the end of this lne of code ‘endl’, which is also part of the ‘std’ namespace, is used to start a new line in the console.

The drawback of specifying the namespace prior to ‘cout’ and ‘endl’ in this way, if they are used a lot in a program, is that it increases the amount that has to be typed to create the program. To alleviate this, a ‘using’ statement can be utilised to include the namespace in the program, meaning that the ‘std::’ can be dropped prior to ‘cout’ and ‘endl’. This will mean that the above can be re-written as follows.

#include <iostream>

using namespace std;

/* Basic program to display a message
   in the console. */
int main()
{
    // Display a message in the console.
    cout << "Hello World" << endl;

    // Terminate the program.
    return 0;
}

The only time the namespace would now be required to precede ‘cout’, for example, would be if ‘cout’ existed in more than one namespace, therefore clarification would be needed as to which version must be used.

It should be noted that including a namespace in this way means that it incorporates the whole of the namespace and not just the aspects that are needed, in this case, ‘cout’ and ‘endl’. This may not be desirable in a large program as it could slow it down further. There is a way to include just the parts of a namespace that are needed, again by utilising the ‘using’ statement.

#include <iostream>

using std::cout;
using std::endl;

/* Basic program to display a message
   in the console. */
int main()
{
    // Display a message in the console.
    cout << "Hello World" << endl;

    // Terminate the program.
    return 0;
}

Going forward here, the namespace will be included as a whole for the sake of simplicity.

When dealing with output, the two left angle brackets together are referred to as an ‘insertion operator’. They are used to output what follows them to the console. In the above examples they output the message, ‘Hello World’, followed by a new line. They can be used to concatenate, or join, multiple strings together, for example.

cout << "Hello" << " " << "World" << endl;

The above concatenates the word ‘Hello’, follows by a space and then the word ‘World’ together, to output the same message, ‘Hello World’, as in the above examples.

Finally, the ‘return’ statement terminates the ‘main’ function and therefore the program as a whole.

In terms of compiling and running the program, this will vary depending on the software being used to create it. The Code::Blocks IDE, mentioned previously, has a ‘Build’ menu, which contains a ‘Build’ option to compile the program and a ‘Run’ option to execute the program. There is also a ‘Build and Run’ option that completes both steps. If software is being used that doesn’t come with a compiler, then one will need to be installed separately and compilation may have to be done via the command line.