4.12 Derived Types

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

A derived type is defined in terms of another type called its parent type, by writing

type My_Type is new Parent_Type range Hi..Lo;
-- where the range constraint is optional.

The new type "inherits" properties from the parent, such as components and primitive operations. A family of types derived from a common root (children, grandchildren, etc) are known as a derivation class. As suggested by the reserved word new, My_Type is a brand new type. Values of the new type cannot by intermixed with values of the parent type or other types derived from the parent, unless explicit type conversions are used. (This is very different from the situation with subtypes, where the word new is not used and the subtype values are legal values of the base subtype.)

Actually, there are two kinds of derived types in Ada.

Simple derived types support a limited form of inheritance, allowing us to add new operations to those inherited from the parent type, but not allowing us to add new components to the data structure. (Adding new components is referred to as type extension -- a key part of OOP.)

Example Program Illustrating Simple Derived Types

The package Earth_Travel exports three types and three functions. The first two types are both derived from the built-in type, Float. The third, Position_Type, is a record type whose components are of the first two, Latitude-Type and Longitude_Type. The Cos_Lat function adds to the inherited intrinsic operations of Float. The other two functions have parameters of Position_Type. The diagram shows dependencies on various resources of the predefined environment.

Im4-13.gif (6718 bytes)

The predefined package Ada.Numerics.Elementary_Functions provides the value of Pi, and the Cos function.

Source Code Listing

------------------------------------------------------------------
-------------------------- Earth_Travel --------------------------
--  This package provides functions that compute distances in the 
--  North-South and East-West directions between two locations 
--  defined by their Latitudes and Longitudes. 
------------------------------------------------------------------
package Earth_Travel is
    
  type Latitude_Type  is new Float range -90.0..90.0;   -- derived
  type Longitude_Type is new Float range -180.0..180.0; -- derived
  
  type Position_Type is
    record
      Longitude : Longitude_Type;
      Latitude  : Latitude_Type;
    end record;
                                          
  function Cos_Lat(Lat : Latitude_Type)  -- adds new operation to 
          return Float;                  -- "intrinsic" +,-,*,/
                                         -- of the parent type of
                                         -- Latitude_Type
  function NS_Distance(P1, P2 : Position_Type) return Float;
  function EW_Distance(P1, P2 : Position_Type) return Float;
end Earth_Travel;
-------------------------------------------------------------------
with Ada.Numerics;                        -- needed for Pi
use  Ada.Numerics;
with Ada.Numerics.Elementary_Functions;   -- needed for Cos
use  Ada.Numerics.Elementary_Functions;
package body Earth_Travel is
          
  NMPerDeg   : constant Float := 60.0;  -- Nautical Miles per Degree
  DegToRad   : constant Float := Pi/180.0;
  Delta_Lat  : Latitude_Type;
  Delta_Long : Longitude_Type;
  -----------------------------------------------------
  function Cos_Lat(Lat : Latitude_Type) return Float is
  begin
    return Cos(Float(Lat)*DegToRad);          -- type conversion
  end Cos_Lat;
  ------------------------------------------------------------
  function NS_Distance(P1, P2 : Position_Type) return Float is
  begin
    Delta_Lat := abs(P1.Latitude - P2.Latitude);
    return Float(Delta_Lat)*NMPerDeg;         -- type conversion
  end NS_Distance;            
  ------------------------------------------------------------
  -- Computes East-West distance at Latitude of P1
  function EW_Distance(P1, P2 : Position_Type) return Float is
  begin
    Delta_Long := abs(P1.Longitude - P2.Longitude);
    return Cos_Lat(P1.Latitude)*
           Float(Delta_Long)*NMPerDeg;        -- type conversion
  end EW_Distance;       
  ------------------------------------------------------------ 
end Earth_Travel; 
--------------------------------------------------------------------
----------------------------- Test_Travel --------------------------
--  This procedure invites the user to specify two sets of Latitude-
--  Longitude pairs, and then calls upon the Earth-Travel functions 
--  to compute distances between the specified locations. 
--------------------------------------------------------------------
with Earth_Travel;
with Ada.Text_IO, Ada.Float_Text_IO; 
use  Ada.Text_IO, Ada.Float_Text_IO;
procedure Test_Travel is
    
  package ET renames Earth_Travel;
    
  Long1, Long2 : ET.Longitude_Type;
  Lat1, Lat2   : ET.Latitude_Type;
  Pos1, Pos2   : ET.Position_Type;
  Distance_NS  : Float;
  Distance_EW  : Float;
  Input_Value  : Float;
 
begin                             
  Put("Enter first Longitude  ");  Get(Input_Value);
  Long1 := ET.Longitude_Type(Input_Value);
  Put("Enter first Latitude   ");   Get(Input_Value);
  Lat1 := ET.Latitude_Type(Input_Value); 
  Put("Enter second Longitude ");  Get(Input_Value);
  Long2 := ET.Longitude_Type(Input_Value);
  Put("Enter second Latitude  ");   Get(Input_Value);
  Lat2 := ET.Latitude_Type(Input_Value); 
  New_Line;
  
  Pos1 := (Long1, Lat1);
  Pos2 := (Long2, Lat2);
  
  Distance_NS := ET.NS_Distance(Pos1, Pos2);
  Distance_EW := ET.EW_Distance(Pos1, Pos2);
  
  Put("The North-South Distance is ");
  Put(Distance_NS); Put(" Nautical Miles"); New_Line;
  Put("The East-West Distance is ");
  Put(Distance_EW); Put(" Nautical Miles"); New_Line;
      
end Test_Travel;
--------------------------------------------------------------------

Here is a sample output from the above program:

     Enter first Longitude  20.0
     Enter first Latitude   20.0
     Enter second Longitude 30.0
     Enter second Latitude  30.0

     The North-South Distance is 6.00000E+02 Nautical Miles
     The East-West   Distance is 5.63513E+02 Nautical Miles

Related Topics

4.1 Type System Overview 5.1 Concepts and Motivation
5.3 Type Extension 5.4 Classwide Types and Dispatching

[ Back to top of pagePrev ] Next ]