5.4 Implementing the Operations

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

Package Bodies for the Previous Package Declarations

This diagram provides a clickable green element for each package body corresponding to the seven package declarations of the preceding page. These bodies contain the implementations of the operations promised by the package declarations.

Im5-5.gif (4517 bytes)

In the seven package bodies listed below there are a total of ten procedures implemented. In all but the first procedure, the essential action is simply to display a line of text using the Put_Line procedure exported by package Ada.Text_IO.

Source Code Listing

---------------------------------------------------------------------
--  These package bodies implement the operations of the types 
--  defining the hierarchy of military vehicles. 
---------------------------------------------------------------------
-------  Vehicles body  ---------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles is
  procedure Rename_Reset(V   : in out Vehicle'Class;
                         Nm  : in Str_2;
                         Lat : in Lat_Type;
                         Lon : in Lon_Type;
                         Hdg : in Hdg_Type) is
  begin
    V.Name      := String(Nm);
    V.Latitude  := Lat;
    V.Longitude := Lon;
    V.Heading   := Hdg;  
  end Rename_Reset;
  ---------------------------------------------------
  procedure Report_Position (V : in Vehicle'Class) is
  begin
    Put_Line(V.Name & " Latitude  = " &
        Integer'Image(Integer(V.Latitude)) &  
        "   Longitude = " &
        Integer'Image(Integer(V.Longitude)));
  end Report_Position;
end Vehicles;
---------------------------------------------------------------------
-------  Airplanes body  --------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles.Airplanes is
  procedure Change_Altitude(A   : in out Airplane'Class;
                            Alt : in Alt_Type) is
  begin
    A.Altitude := Alt;  
    Put_Line("Airplane " & A.Name & 
      " climbs/dives to Altitude = " & 
      Integer'Image(Integer(A.Altitude)));  
  end Change_Altitude;
  ----------------------------------------------
  procedure Change_Heading (A : in out Airplane;
                            H : in Hdg_Type) is 
  begin
    A.Heading := H;
    Put_Line("Airplane " & A.Name & 
             " banks and turns to Heading = " &
             Integer'Image(Integer(A.Heading)));  
  end Change_Heading;
end Vehicles.Airplanes;
---------------------------------------------------------------------
-------  Ships body  ------------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles.Ships is
  procedure Change_Heading (S : in out Ship;
                            H : in Hdg_Type) is 
  begin
    S.Heading := H;
    Put_Line("Ship " & S.Name & 
             " using rudder, turns to Heading = " &
             Integer'Image(Integer(S.Heading)));  
  end Change_Heading;
end Vehicles.Ships;
---------------------------------------------------------------------
-------  Fighters body  ---------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles.Airplanes.Fighters is
  procedure Deploy_Weapon(F : in Fighter) is
  begin
    Put_Line("Fighter " & F.Name & 
             " fires anti-aircraft missile");  
  end Deploy_Weapon;
end Vehicles.Airplanes.Fighters;
---------------------------------------------------------------------
-------  Bombers body  ----------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles.Airplanes.Bombers is
  procedure Deploy_Weapon(B : in Bomber) is
  begin
    Put_Line("Bomber " & B.Name & 
             " releases bomb over target");  
  end Deploy_Weapon;
end Vehicles.Airplanes.Bombers;
---------------------------------------------------------------------
-------  Cruisers body  ---------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
package body Vehicles.Ships.Cruisers is
    
  procedure Deploy_Weapon(C : in Cruiser) is
  begin
    Put_Line("Cruiser " & C.Name & 
             " launches a cruise missile");  
  end Deploy_Weapon;
end Vehicles.Ships.Cruisers;
---------------------------------------------------------------------
-------  Submarines body  -------------------------------------------
---------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;          
package body Vehicles.Ships.Submarines is
  procedure Change_Depth(Sub : in out Submarine;
                           D : in Depth_Type) is
  begin
    Sub.Depth := D;
    Put_Line("Submarine " & Sub.Name &
             " dives/climbs to Depth = " & 
             Integer'Image(Integer(Sub.Depth)));  
  end Change_Depth;
  ----------------------------------------------
  procedure Deploy_Weapon(Sub : in Submarine) is
  begin
    Put_Line("Submarine " & Sub.Name &
             " fires a torpedo");  
  end Deploy_Weapon; 
end Vehicles.Ships.Submarines; 
--------------------------------------------------------------------

The next page provides a test procedure that is used to exercise the code given above and in the previous section.

Related Topics

5.1 Concepts and Motivation

[ Back to top of pagePrev ] Next ]