Below is the skeleton of a simple package, firstly its specification file.
and now its body, or implementation file. The most important note is the keyword body, forget this and you will find the compiler will become rather upset at finding subprogram bodies in a package specification!.
As you can see the package specification contains nothing you have not seen, a type declaration, an exception and three simple subprograms. However in the package body you will notice an object called Trading_State which holds the current state, this is not accessible to any client of the package Ada_Store_Trading This is equivalent to an object in C which is marked static within a code module.
You should also have noticed that the package itself has a statement part, in which it sets the initial state of the object Trading_State. This code part may be used to open files, perform initialisation, and in some cases nearly all the work is done here, with functions and procedures used by the package simply to access data set up during this initialisation (see section 4.6.1 for discussion on initialisation).
This package initialisation should solve the sort of problem which causes code like the following to be found frequently in a C source module:
/* source module */ struct something important_data; int important_data_initialised = 0; void function_1(void) { if (!important_data_initialised) { /* initialise important_data */ } }
A number of strategies exist in C++, usually using a class to represent the important data and using its constructor to initialise it correctly. The Ada approach is more flexible and is certainly easier to read and understand as most of the C and C++ solutions are cobbled togethe
Copyright ©
1996 Simon Johnston &
Addison Wesley Longman