18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Polymorphism 235<br />

16.5 A build<strong>in</strong>g <strong>in</strong>formation program<br />

A class Build<strong>in</strong>g, which is used as a conta<strong>in</strong>er to store and retrieve details about the accommodation <strong>in</strong> a<br />

build<strong>in</strong>g, has the follow<strong>in</strong>g responsibilities:<br />

Method<br />

Add<br />

About<br />

Responsibility<br />

Add a description of a room.<br />

Return a description of a specific room.<br />

The <strong>Ada</strong> specification for the class Build<strong>in</strong>g is:<br />

with Class_Room, Class_Room.Build;<br />

use Class_Room, Class_Room.Build;<br />

package Class_Build<strong>in</strong>g is<br />

type Build<strong>in</strong>g is tagged private;<br />

procedure Add( The:<strong>in</strong> out Build<strong>in</strong>g; Desc:<strong>in</strong> P_Room );<br />

function About(The:<strong>in</strong> Build<strong>in</strong>g; No:<strong>in</strong> Positive) return Str<strong>in</strong>g;<br />

private<br />

Max_Rooms : constant := 15;<br />

type Rooms_Index is range 0 .. Max_Rooms;<br />

subtype Rooms_Range is Rooms_Index range 1 .. Max_Rooms;<br />

type Rooms_Array is array (Rooms_Range) of P_Room;<br />

type Build<strong>in</strong>g is tagged record<br />

Last : Rooms_Index := 0; --Last slot allocated<br />

Description : Rooms_Array; --Rooms <strong>in</strong> build<strong>in</strong>g<br />

end record;<br />

end Class_Build<strong>in</strong>g;<br />

The procedure Add adds new data to the next available position <strong>in</strong> the array.<br />

package body Class_Build<strong>in</strong>g is<br />

procedure Add( The:<strong>in</strong> out Build<strong>in</strong>g; Desc:<strong>in</strong> P_Room ) is<br />

beg<strong>in</strong><br />

if The.Last < Max_Rooms then<br />

The.Last := The.Last + 1;<br />

The.Description( The.Last ) := Desc;<br />

else<br />

raise Constra<strong>in</strong>t_Error;<br />

end if;<br />

end Add;<br />

Note:<br />

The exception Constra<strong>in</strong>t_Error is raised if there is no more free space <strong>in</strong> the array.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!