Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-wpx69 Total loading time: 0 Render date: 2024-09-27T03:40:25.097Z Has data issue: false hasContentIssue false

12 - Packages and Private Types

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

The previous chapters described the small-scale features of Ada in some detail. These features correspond to the areas addressed by the pioneering languages prior to about 1975. This chapter and the next discuss the important concepts of abstraction and programming in the large which entered into languages generally around 1980. The more recent concepts of type extension, inheritance and polymorphism are dealt with in Chapter 14.

In this chapter we discuss packages (which is really what Ada is all about) and the important concept of a private type. The topic of library units and especially library packages is discussed in detail in the next chapter.

Packages

In this section we introduce the concept of an Abstract State Machine (ASM) and in the next section we extend this to Abstract Data Types (ADT). The general idea of abstraction is to distinguish the inner details of how something works from an external view of it. This is a common idea in everyday life. We can use a watch to look at the time without needing to know how it works. Indeed the case of the watch hides its inner workings from us.

One of the major problems with early simple languages, such as C and Pascal, is that they do not offer enough control of visibility. For example, suppose we have a stack implemented as an array plus a variable to index the current top element, thus

Max: constant := 100;

S: array (1 .. Max) of Integer;

Top: Integer range 0 .. Max;

We might then declare a procedure Push to add an item and a function Pop to remove an item.

procedure Push(X: in Integer) is

begin

Top := Top + 1;

S(Top) := X;

end Push;

function Pop return Integer is

begin

Top := Top - 1;

return S(Top + 1);

end Pop;

In a simple block structured language it is not possible to have access to the subprograms Push and Pop without also having direct access to the variables S and Top. As a result we can bypass the intended protocol of only accessing the stack through calls of Push and Pop. For example we might make use of our knowledge of the implementation details and change the value of Top directly without corresponding changes to the array S.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×