You are a professional programmer using C and C++ to get a job done, you know the strengths and weaknesses of the language you use. How about if I were to offer you a tool which could almost eradicate all those pointer problems for you (memory leaks, dangling pointers), solve all those array bounds problems for you (running off the end of arrays) and eradicate all those header file inconsistency problems for you. You've seen, as I have, a number of tools you can use to improve the safety and reliability of you code, you run them periodically to report a file or two. This tool will work on your whole project, it runs every time you compile (at no extra cost!) and will guarantee that you cannot proceed until you have fixed problems. What is this new tool? - an Ada compiler; start writing your next project in Ada95 and reap the benefits of static safe typing backed up with dynamic run-time checking and you have a programming environment which will improve the reliability of your software and your faith in it.
Ada, as a programming language, has had somewhat of a bad press; its detractors claim it is overly verbose, too complex and technically dated. Ada has however always been a powerful force in the computing community, it is was specifically developed to support software engineering principles with safety and reliability as prime concerns. Ada proved that object-based software could be developed in a performance critical environment at a time when object oriented programming was being criticised for being too resource hungry. Ada also pioneered the main-stream use of generic programming long before C++ introduced templates.
With the revision process which has provided us with the Ada95 programming language Ada has come of age. The addition of true object oriented programming techniques, interfaces to other programming languages and a wealth of other important alterations has left us with a language which allows us to get our work done with the minimum of fuss and the maximum confidence the resulting applications. Ada has come out of its traditional military embedded systems and has hit the high street!
To sum up the philosophy of those working on the revision process I have included below the first paragraph from the introduction of the Ada95 Language Reference Manual:
"Ada was originally designed with three overriding concerns: program reliability and maintenance, programming as a human activity, and efficiency. This revision to the language was designed to provide greater flexibility and extensibility, additional control over storage management and synchronisation, and standardised packages oriented toward supporting important application areas, while at the same time retaining the original emphasis on reliability, maintainability and efficiency."
The development of this book has progressed through three distinct phases, firstly it was a simple collecting of e-mails and Internet news items which discussed C++ and Ada95. These discussions covered areas such as comparisons of language features and interfacing Ada and C/C++ code. I tried to organise this collection into some form of frequently asked question list with my own questions and answers as well. Before I completed the list it dawned on me that this might be of use to other C++ programmers hoping to benefit from using Ada, so I re-wrote it as a tutorial and published it on the Word Wide Web (here is a copy). Since making it available I have received a number of requests and comments included one which suggested that I consider writing a book.
The result of a lot of work and even more questions has produced a book which will help professional C++ programmers to understand the Ada95 programming language and see where it will benefit them as software engineers. It is important to remember that Ada95 is just a programming language, it is not a cure-all and does not do your work for you. The course of this book expects that the reader is familiar with programming complex systems, usually in C or C++ and has some experience in object oriented programming, possibly some systems programming, and possibly some experience with a multi-threaded environment.
New topics will be introduced wherever possible in C++ terminology and then using the Ada terms and semantics so that not only is the programming language taught, but also the culture and background.
The book is in two parts, the first covers the core language with its structures, types and organisation. The second part focuses on the library and the Ada95 specialised needs annexes, sections which provide optional support for certain specialist application areas.
Part 1 - The Core Language
Chapter 1 - Introduction
This brief chapter includes the history of the Ada programming language and a short tour of language features. It also introduces and examines the examples and the on-going case study used in the text.
Chapter 2 - Language Building Blocks
We will start our tour of Ada with the control structures and operators provided by the language. This section should hold no great surprises for us, at the level of simple statements the two languages are very similar.
Chapter 3 - Ada Types
This chapter provides a description of the standard types and more importantly the general type model which underpins Ada's mechanisms supporting abstract data types and object oriented programming. C++ has brought strong typing to C programmers but the type checking in Ada is far stricter.
Chapter 4 - Packages
Packages are a feature found in a number of languages such as Modula-2, Modula-3[18] and Oberon[17] (in these languages they are called modules). They provide name spaces and are an important feature for designing Abstract Data Types (ADTs) and object-based designs.
Chapter 5 - Generics
Like templates in C++ Ada provides a method for producing parameterised subprograms which can then be instantiated for a number of argument types. Ada also allows us to develop generic packages for parameterised types. This all leads to the provision of more general types and algorithms to support a high level of reuse.
Chapter 6 - Tagged types
Ada has always allowed object based design and programming (based around abstract data types). Tagged types are the Ada95 facility for true object oriented programming. Tagged types provide inheritance and polymorphism and are as powerful as classes in C++, though their different implementation can be frustrating for C++ programmers new to Ada.
Chapter 7 - Tasking
Ada provides portable language features for concurrent processing within a single program. This facility is similar to the facilities called threads or lightweight processes found in many modern operating systems such as OS/2, Windows-95, Windows-NT or Solaris.
Ada95 has extended some of these features and added a passive thread safe component called a protected type.
Part 2 - The Library and Annexes
Chapter 8 - The Ada Library
The standard library defined for Ada83 and Ada95 is vast. A number of common topics are covered, including IO and mathematical operations. One very strong feature of the Ada95 library is its string handling. Although introduced in the section on the string type itself a more detailed tour will be presented here.
Chapter 9 - The Annexes
The annexes contain additional features which, in some cases, may be left unimplemented by compiler vendors.
The exception is the annex describing features to interface Ada to code written in other languages, this annex is required.
The description of the core language will attempt to cover as much common ground as possible to start with so that change is brought in as slowly as possible. However this does not mean that I have shied away from introducing Ada features for which there is no C++ equivalent, this is not a comparison of the two languages. Many modern operating systems for example contain constructs known either as lightweight processes or as threads which allow programmers to have multiple threads of execution within the same address space, Ada has a model of concurrency in the core language itself which means that you can write applications using multiple threads which will operate even on operating systems which do not directly support threads. The tour of the standard library and annexes provides further knowledge allowing you to apply the language to complex problems with the minimum of development by introducing extensive string handling, file and text input/output, mathematics libraries, systems programming support, real-time systems support amongst others.
I have used the standard Ada documentation conventions; code will be in a courier font, like this with keywords in bold, like this. The C++ code will also appear in the courier font, but no keyword highlighting will be done, this is usual in C++ documentation. I will also include as many references to the Ada Language Reference Manual[1] as possible so that you may read over a topic in more detail if you want.
Some topics however may not be covered as fully here as in other texts, especially [6] and [7], because I hope to present a book which will help to convert your professional skill and knowledge of one high level language, C++, into marketable skill in Ada95. I will attempt to avoid getting into detailed language design issues and focus on the information required to perform everyday tasks. This has led to a somewhat terse style because I am assuming that my readers have already learned the science and art of software design and implementation and are more interested in solving problems than the pseudo-religion of programming language design. I will also include as many references to the Ada Language Reference Manual[1] as possible so that you may read over a topic in more detail if you want.
As we go through the book we will be working on fragments of a single case study, though in many cases smaller stand-alone examples will have to suffice in the earlier chapters as we will not have covered enough to work with real code. We may have to re-implement the work 2 or 3 times, but such re-implementation of code using more appropriate techniques is part of our everyday existence as programmers. The case study itself is taken from work which I have done with ICL Retail Systems, although it is not based directly on any work made commercially available by them. The completed example is available on the companion CD or from my web site, and contains more than I managed to get into this book. If you have comments, queries or improvements to the application, drop me a line and I will maintain an extended copy as well!
The Companion CD-ROM
Thanks to Aonix the companion CD-ROM contains a complete Ada 95 compiler environment for the Windows NT and Windows 95 operating systems. Aonix have also provided a number of useful resources other than the compiler and it's tutorial.
The Ada 95 Language Reference Manual
This text is the official language definition document for the Ada programming language. It is clear, concise and as part of the open development process of the Ada language printable copies are made freely available.
This free access promotes the use of the text, where for example the C++ equivalent [10] is expensive and rarely used by programmer
The Ada 95 Rationale
This is a document which accompanies the language reference manual and describes why the language is the way it is, some of the decisions involved in the language design and more in the way of examples than the reference manual. Again you will find nearly all Ada programmers have access to the Rationale as it is also distributed free.
All the examples are provided in source form on the CD, and should compile with any version of the GNAT compiler, though some packages may contain operating system dependant code. I have also committed to keeping an up-to date copy on my web site at http://www.acm.org/~skj/CppToAda/examples.htm.
All examples have been compiled using the Aonix compiler and also the GNAT Ada 95 compiler for Win32 from Labtek at Yale University.
Other Internet resources which are useful to all Ada programmers include the Home of the Brave Ada Programmers World Wide Web site at http://www.adahome.com/ which will contain many of the resources from this CD but is also one of the best places to start looking for general Ada information. I also mentioned above the news group comp.lang.ada which is alive with debate on the language itself, help with common problems and is frequented by most of the acknowledged experts on the language.
Simon K. Johnston.
http://www.acm.org/~skj/
skj@acm.org
Copyright ©
1996 Simon Johnston &
Addison Wesley Longman