Table Of Contents

Previous topic

LAL-info documentation

Next topic

2. First steps with subversion (svn)

Download

1. Introduction to C++

spot

This section will introduce:

  • the software configuration management,
  • the C++ environment,
  • how to put in place a work flow.

The red line of this section is the implementation - in C++ - of an application reading files holding either a complete image or the definition of shapes which will be used later on during the hands-on session.

Such an image file can be found there while a file storing shapes can be found here. You should look at these files to familiarize yourself with their structure.

  • We’ll proceed in a step-by-step fashion: it is therefore very important to make sure you go through all these stages.
  • You should be extremely conscious about your style, that is, code clarity:
    • define, use and follow coding conventions (i.e. code presentation)
    • give sensible names to variables and functions
    • properly document your code.
  • Finally, it is paramount to regularly test your code to ensure a proper progression thru the exercize(s). In practice, this means:
    • systematically test your code after having implemented each and every feature
    • once the feature has been verified to work properly, do not hesitate to improve the clarity of your code (by e.g. refactor it or reformat it) and thus re-test it systematically after such a modification (“if it is not tested, it’s broken”)

1.1. Step 1: prepare your workarea (15 mins)

This first steps allows you to define and create your workarea with a tool to automatize tasks.

  • CMT: we use a software configuration management tool - CMT - to automatize the necessary and needed steps to rebuild an application (which is bound to evolve and become more and more complicated and involved).
  • Recommanded editors: available in /Applications such as Emacs: emacs-icon
  • Terminal: available from the dock (bottom of the screen), click on the Terminal icon: dock
  • Structure and layout of your workarea:

    • create a work directory under your home directory, called Project (Don’t choose another name in order to stay consistent with this documentation)
    • in the following, the $> stands for the prompt after which one can issue UNIX commands:
    $> cd
    $> mkdir Project
    

Throughout this hands-on session, you should make sure to properly organize your workarea by creating -if necessary- proper directories. This allows to isolate and decouple development areas from one another, minimize naming conflicts (e.g. files automatically generated by some tools with the same name) and this dramatically eases groking the overall structure of the project.

One could also create a directory Project/Tests in which one would quickly evaluate and test a snippet of code extracted from the documentation.

_images/validate-step.jpg

Validate this step


1.2. Step 2: building a simple application (1h)

This step illustrates the simple development cycle of an application


After having bootstrapped the workarea environment, this cycle holds the following steps:

  1. source code editing
  2. compilation and linking steps
  3. execution of the application
  4. test the application (or a subset of its components)

Generally speaking, the life cycle of an application can be described by cyclically iterating through the above mentionned steps. For example, the typical cycle consists in doing 1 (once) and then the sequence (2,3,4) in a loop.

1.2.1. Bootstrapping the workarea environment

Firstly, go under your work directory Project.

$> cd ~/Project

Warning

all your work shall happen under this Project directory (or one of its subdirectory.)

We will then setup a workspace with CMT dedicated to this exercize. This workspace shall be named Hello.

$> cmt create Hello v1
------------------------------------------
Configuring environment for package Hello version v1.
CMT version v1r20p20090520.
Root set to /Users/visiteur/tests.
System is Darwin
------------------------------------------
Installing the package directory
Version directory will not be created due to structuring style
Installing the cmt directory
Installing the src directory
Creating setup scripts.
Creating cleanup scripts.

Warning

a cmt directory has been created as a side-effect of running the cmt create command. All the remaining work of this exercize shall be caried from under that directory.

$> cd Hello/cmt

1.2.2. Editing

We will copy the program given in example below under the directory ../src of our workspace previously created. This will became a hello.cpp file.

After a close inspection of the sources, we notice it is the classical program which displays a simple message on the screen. For the moment, you shouldn’t try too hard to understand what is performed on each line of this program.

/**
 * @file hello.cpp
 * @author LAL ens <ens@lal.in2p3.fr>
 * @date   March 2007
 *
 * First steps with a development environment.
 * First program hello.cpp: display "Hello!" on standard output.
 */

#include <iostream>

int main(int argc, char **argv)
{
  std::cout << "Hello!" << std::endl;

  return 0;
}

Nevertheless, you should notice:

  • the #include command, whose character # shall be placed at column number 1 of your source file. This command allows to include the content of another file in your program. Here, we include the content of the file iostream whose location is known to the compiler and which holds the declaration of the prototypes of the functions used of inputs/outputs.
  • the comments following the Doxygen format, labelled with the /** and */ tokens

1.2.4. Execution and tests

If all went well, we can now run and test our new application:

$> ../Darwin/hello.exe
Hello!
_images/validate-step.jpg

Validate this step


1.3. Step 3: Familiarization with C++ (2h)

We’ll modify the hello.cpp file by introducing a while loop which will display the previous welcome message until a correct answer is given. This correct answer will terminate the program.


A few more precisions, in addition to these informations:

  • right now don’t use using namespace std; but declare explicitly the namespace for each STL object;
  • std::cout is the standard output. One uses the << operator to write into std::cout;
  • std::cin is the standard input. One uses the >> operator to read from std::cin;
  • std::endl is the manipulator allowing to insert an end-of-line character (insertion of such a character isn’t implicit, you have to do it manually);
  • use a boolean variable (of type bool whose values are either true or false) to manage the while loop;
  • use variable of type std::string for the array of characters (std::string is defined in the <string> header file)
  • the std::string equality comparison operator in C++ is ==
  • the operator to test if 2 std::strings are not equivalent is !=;
  • the logical operators AND and OR are, respectively, && and ||.

The building and testing of the application is performed as in the previous stage.

You should make sure to update the comments inside the program.

Typically, one should get the following output:

$> ../Darwin/hello.exe
Hello!
continue? yes
Hello!
continue? yes
Hello!
continue? yes
Hello!
continue? yes
Hello!
continue? no
Bye.
_images/validate-step.jpg

Validate this step


1.4. A few references

These might help in case you are a bit lost with C++:

  1. A C++ reference site
  2. Lectures from J.F Rabasse.
  3. An introduction to the C++ language is available.
It is by no means exhaustive but is a short introduction to a few elements of C++ needed throughout this hands-on.