1.2. Hello LEDA world! - The very first program

Learning objectives

The LEDA header files
The namespace leda

Let's start with the most important of all programs, the famous Hello world! program, which since the book “The C Programming Language” is regarded as the standard program used in teaching a new programming environment and testing its operability. A friendly computer scientist recently said to the author: “Yes, if only Hello world! runs, the rest is no longer hard, no matter what the rest is.” So let us make it our first task to write Hello world! with the help of LEDA and to make it run, so that we may be able to tackle the rest of this tutorial in a cozy and relaxed way.

Since LEDA is a library of data types and algorithms, this means that we must use at least one data type or at least one algorithm of LEDA to be able to call a program a LEDA program. Here we use the LEDA type string to store the character string Hello LEDA world!. The class string itself is described in greater detail in Section 2.1. Here is the program:

Filename: HelloLEDAWorld.C
#include <LEDA/string.h>
#include <iostream>

int main() 
  leda::string msg = "Hello LEDA world!";

  std::cout << msg << "\n";

The header files of LEDA

The class declarations of the LEDA data types are contained in header files. To be able to use a certain data type, we must include the appropriate header file. In general, the header file belonging to the LEDA type X is called X.h.[2] According to this we include here the file string.h:


The namespace leda

LEDA defines all identifiers of types, functions, constants, etc., in the namespace leda.[3] Thereby LEDA data types can be used together with types from other libraries like ABACUS, CGAL, and primarily the C++ standard library (and the former STL now merged into the latter one).

There are three possibilities of using identifiers from the namespace leda for application programs:

  1. Specifying an identifier fully qualified with the prefix leda::, for example like in the above program by a declaration

    leda::string msg;

  2. Making an identifier from a namespace available by a using declaration, for example by

    using leda::string;

  3. Making all identifiers from the namespace leda available by a using directive, for example by

    using namespace leda;

Another possibility is therefore the following one:

Filename: HelloLEDAWorld2.C
#include <LEDA/string.h>
#include <iostream>

using leda::string;
using std::cout;

int main() 
  string msg = "Hello LEDA world!";

  cout << msg << "\n";

Which of these possibilities we should choose differs from program to program. The use of using directives is generally regarded as bad style because the programmer loses the overview on which identifiers he makes available (in the case of a using namespace leda these are quite a lot) and because the danger of name collisions is increased thereby. using directives make sense primarily when old LEDA programs in which identifiers are not yet specified qualified shall be linked against LEDA versions with a release number as of 4.4.

In the following, we will usually make clear explicitly by using declarations at the beginning of a program which identifiers we make available. In doing so we also highlight with which identifiers of LEDA we mainly deal in the section in question. We usually use identifiers from other namespaces, such as std::cout, fully qualified unless they occur very frequently in the program text so that a using declaration saves paperwork.

[Warning] Warning

Both of the C++ standard library and LEDA contain a class string. A mistake which is made particularly often when adapting old code which does not yet use namespaces to new code which is working with namespaces is the following: Two using directives lead to an ambiguity (name collision) of the identifier string, such as in the following code fragment:

using namespace leda; 
using namespace std;
string msg; // ambiguous: std::string or leda::string?

Since we use identifiers from the standard library only occasionally in the following programs, we will almost always specify them fully qualified or make them available by a using declaration.

The rest of the program should be self-explanatory. A LEDA string can be initialized with a constant C++ string literal and written to standard output by means of the operator <<.

There you are! This was already almost the most important thing to say about LEDA. Now we must only make it run and look at the other data types thereafter.

[2] There are exceptions to this rule. For example, the declaration of the class dynamic_random_variate is found in the file LEDA/random_variate.h. In every case, the manual page belonging to a data type gives information about the header file to be included

[3] This is valid since version 4.4. There was no namespace leda in older versions; all LEDA identifiers can be specified unqualified in these versions. Only if code from other libraries is to be used in the same program, the prefix leda_ must be set in front of every LEDA identifier and the macro LEDA_PREFIX must be defined when compiling.

For example, in the above program one would write

leda_string msg = "Hello LEDA world!";

and the gnu C++ compiler would have to be invoked with the option -DLEDA_PREFIX.