5.5 OOP Static Example

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

Example Program with a Tagged Type Hierarchy

This program withs in the tagged type hierarchy -- the packages given in the previous two sections. Four arrays of vehicles (fighters, bombers, cruisers and submarines) are statically created in the declarative part of the main procedure. In the executable part, a set of non-dispatching calls are made, followed by a set of dispatching calls.

Im5-6.gif (5178 bytes)

The dispatching operations in the code listed below are statically dispatched because the controlling actual parameters (components of FA, BA, CA and SA) are declared as objects of specific types (Fighter, Bomber, Cruiser and Submarine) rather than classwide types. (The example in the following section illustrates dynamically dispatched operations.)

Source Code Listing

-------------------------------------------------------------------
--  This procedure tests the derivation class rooted at Vehicle 
--  by declaring arrays of vehicles and making a series of 
--  non-dispatching and dispatching calls to individual objects.
----------------------------------------- --------------------------
with Vehicles, Vehicles.Airplanes, Vehicles.Ships; 
use  Vehicles, Vehicles.Airplanes, Vehicles.Ships;
with Vehicles.Airplanes.Fighters, Vehicles.Airplanes.Bombers;
use  Vehicles.Airplanes.Fighters, Vehicles.Airplanes.Bombers;
with Vehicles.Ships.Cruisers, Vehicles.Ships.Submarines;
use  Vehicles.Ships.Cruisers, Vehicles.Ships.Submarines;
procedure Test_Vehicles is
                                        -- vehicle declarations
  FA : array (1..3) of Fighter; 
  BA : array (1..3) of Bomber;
  CA : array (1..3) of Cruiser;
  SA : array (1..3) of Submarine;
begin
                                        -- non-dispatching calls
  Rename_Reset(V=>FA(2), Nm=>"F2", Lat=>25, Lon=>55, Hdg=>60);
  Rename_Reset(V=>BA(1), Nm=>"B1", Lat=>30, Lon=>60, Hdg=>90);
  Rename_Reset(V=>CA(1), Nm=>"C1", Lat=>30, Lon=>60, Hdg=>0);
  Rename_Reset(V=>SA(2), Nm=>"S2", Lat=>25, Lon=>55, Hdg=>45);
  Report_Position(CA(1));
  Report_Position(FA(2));
  Report_Position(SA(2));
  Report_Position(BA(1));
                                        -- dispatching calls
  Change_Heading (CA(1), 45);                  
  Deploy_Weapon  (CA(1));  
  Change_Heading (FA(2), 70);
  Deploy_Weapon  (FA(2));
  Change_Depth   (SA(2), 50);
  Deploy_Weapon  (SA(2));    
  Change_Altitude(BA(1), 20000);
  Deploy_Weapon  (BA(1));                           
end Test_Vehicles;
-------------------------------------------------------------------

The above program produces the following output:

     C1 Latitude = 30  Longitude = 60
     F2 Latitude = 25  Longitude = 55
     S2 Latitude = 25  Longitude = 55
     B1 Latitude = 30  Longitude = 60
     Ship C1 using rudder, turns to Heading = 45
     Cruiser C1 launches a cruise missile
     Airplane F2 banks and turns to Heading = 70
     Fighter F2 fires anti-aircraft missile
     Submarine S2 dives/climbs to Depth = 50
     Submarine S2 fires a torpedo
     Airplane climbs/dives to Altitude = 20000
     Bomber B1 releases bomb over target

Related Topics

5.3 Classwide Types and Dispatching 5.4 Implementing the Operations

[ Back to top of pagePrev ] Next ]