Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-wpx69 Total loading time: 0 Render date: 2024-10-07T05:17:33.950Z Has data issue: false hasContentIssue false

3 - Abstraction

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

As mentioned in Chapter 1, abstraction in various forms seems to be the key to the development of programming languages. In this chapter we survey various aspects of abstraction with particular emphasis on the object oriented paradigm.

Packages and private types

In the previous chapter we declared a type for the manipulation of a buffer as follows

type Buffer is

record

Data: String(1 .. 80);

Start: Integer;

Finish: Integer;

end record;

in which the component Data actually holds the characters in the buffer and Start and Finish index the ends of the part of the buffer containing useful information. We also saw how the various components might be updated and read using normal assignment.

However, such direct assignment is often unwise since the user could inadvertently set inconsistent values into the components or read nonsense components of the array.

A much better approach is to create an Abstract Data Type (ADT) so that the user cannot see the internal details of the type but can only access it through various subprogram calls which define an appropriate protocol.

This can be done using a package containing a private type. Let us suppose that the protocol allows us to reload the buffer (possibly not completely full) and to read one character at a time. Consider the following

package Buffer_System is -- visible part

type Buffer is private;

procedure Load(B: out Buffer; S: in String);

procedure Get(B: in out Buffer; C: out Character);

private -- private part

Max: constant Integer := 80;

type Buffer is

record

Data: String(1 .. Max);

Start: Integer := 1;

Finish: Integer := 0;

end record;

end Buffer_System;

package body Buffer_System is -- package body

procedure Load(B: out Buffer; S: in String) is

begin

B.Start := 1;

B.Finish := S'Length;

B.Data(B.Start .. B.Finish) := S;

end Load;

procedure Get(B: in out Buffer; C: out Character) is

begin

C := B.Data(B.Start);

B.Start := B.Start + 1;

end Get;

end Buffer_System;

Note how the package comes in two parts, the specification and the body. Basically the specification describes the interface to other parts of the program and the body gives implementation details.

With this formulation the client can only access the information in the visible part of the specification which is the bit before the word private.

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 [email protected] 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.

  • Abstraction
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.006
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.

  • Abstraction
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.006
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.

  • Abstraction
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.006
Available formats
×