Topic 1: Introduction to C++ Program

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.

Integrated Development Environment (IDE)

An integrated development environment (IDE) is a software application that provides necessary tools for computer programmers for software development. It contains both an editor and compiler. Some examples of C++ IDE include Eclipse, CodeLite, Sublime Text, NetBeans, Qt Creator, Brackets, Atom, and Dev C++.

For this guide we are going to use Dev C++. The latest version of the Dev C++ software can be downloaded from the official website: www.bloodshed.net. It is free and open-source software.

Hello world program

  1. Once the program is installed, start Dev-C++ IDE program: Go to START -> Bloodshed Dev-C++ and choose to open Dev-C++
  2. Create a new source file by clicking on the toolbar button or Go to File -> New and choose Source File. Source file will allow you to write codes.
  3. Save the file at an appropriate location by choosing File -> Save As. Organise your files properly in folders.
  4. Type the following C++ code. You should write the code exactly as follows (note that line 8 is return 0 (zero)):

    Hello World Program
    Figure 1.1
  5. Compile the program by choosing Execute -> Compile (or F9). If you have errors try removing the errors.
  6. If there are no errors then run the program by choosing Execute -> Run (or F10).
Note that you can combine steps 5 and 6 by pressing F11.

Output
  • Hello World!

Translating source code into executable code

Preprocessor Directives

A #include directive allows you to merge the source code in one file with that in another file.

The #include is required when using the cin or cout stream objects. It is not a statement, so no semicolon is needed at the end. In C++, all statements must end with a semicolon.

A using directive tells the compiler where in internal memory it can find definitions of C++ keywords and classes like double or string.

The using namespace std; directive indicates that the definitions of the standard C++ keywords and classes are located in the std (standard) namespace. It is a statement, so a semicolon is required at the end. A namespace is a special area in internal memory. Figure 1.2 shows the parts of the program.

...
Figure 1.2
The main() Function

The main function is also called the driver function which controls all the other functions in the program.

It contains the following components:

  1. Header – it is the first line of the function which contains;
    • The type of data returned by the function i.e int
    • The name of the function i.e main
    • The type of data that must be passed into the function when it is invoked also called argument or parameters. i.e. an empty list
  2. Body – the statements inside the function are enclosed in braces. Each statement inside the function must be terminated with a semicolon.
  3. Return – a keyword causing the appropriate value to be returned from the function. A return 0 in the main function causes the program to end.

Compiler saves object code in object files with extension .obj. Linker combines .obj files with other machine code necessary to run the program and produces an executable file with extension .exe. This is shown in Figure 1.3.

...
Figure 1.3