Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-nptnm Total loading time: 0 Render date: 2024-09-28T13:18:34.112Z Has data issue: false hasContentIssue false

21 - Object Oriented Techniques

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

We have now introduced all the main features of Ada 2012 and illustrated them with many examples. An important topic which has arisen in various forms is object oriented programming. Chapter 14 introduced type extension and dynamic polymorphism using tagged types. Chapter 18 discussed type parameterization by discriminants. Chapter 19 covered static polymorphism through genericity. And finally Chapter 20 on tasking discussed concurrent objects which some might feel are the only true objects anyway.

However, the linear exposition has prevented us from exploring a number of important interactions between these features. Accordingly this chapter addresses a number of examples which further illustrate the various techniques and show how they fit together. The next chapter addresses further examples of tasking.

Extension and composition

We start this chapter on object oriented techniques with some general remarks about type composition.

There are a number of ways in which types can be put together. An array type is composed of two other types, the index type and the component type. A record type is composed of the several types of its components. Type extension is a special form of composition in which a record type has the same components as another type plus additional components at the same level.

As an example consider the following two declarations of the type Circle

type Circle is new Object with -- type extension

record

Radius: Float;

end record;

type Circle is -- type composition

record

Obj: Object;

Radius: Float;

end record;

Although both have three eventual components, the structure and naming are quite different. The fundamental difference is between ‘X is a Y’ and ‘X has a Y’. The circle is an object and so the type extension is the appropriate form.

It is interesting to contrast this with

type Cylinder is new Circle with

record

Height: Float;

end record;

briefly introduced in Section 14.1. This type Cylinder is foolish; a cylinder is not a circle although it does have a circle as part of its structure.

In deciding the correct approach, a good test is to check that class wide operations still apply – especially since they cannot be overridden. But clearly Distance is curious because the distance to the centre of the cylinder will depend upon whether we are thinking of the cylinder as standing on the plane or not.

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
×