In this chapter, we presented some introductory material on tagged types. The latter is a very important capability of Ada because it provides inheritance and therefore facilitates object-oriented programming. Closely related to tagged types is general access types, which are pointers that can designate declared variables as well as dynamically allocated ones.
Table 15.1
Construct EffectTagged Type
TYPE MotorVehicle IS TAGGED RECORD Declares a tagged type, which Axles: Positive; can be extended by derivation to EngineSize: Positive; produce dynamic variants Weight: Positive; END RECORD; FUNCTION AxlesOf( M: MotorVehicle) If AxlesOf is declared just below RETURN Positive; MotorVhicle in the same package spec, it is a primitive operation. TYPE TopType IS ( Soft, Hard); TYPE Automobile IS NEW MotorVehicle Automobile is derived from WITH RECORD MotorVehicle and inherits its Doors: Positive; primitive operations Top: TopType; END RECORD; FUNCTION TopOf( A: Automobile) DoorsOf is a primitive operation of RETURN TopType; AutomobileExtension Aggregate
A: Automobile; A := ( MakeVehicle ( Axles => X, A is an Automobile that inherits EngineSize => E, several of its fields from Weight => W) MotorVehicle WITH Doors => 4, Top => Soft);Class-Wide Type
V: MotorVehicle'Class; V can be a MotorVehicle, an Automobile, or anything else derived from MotorVehicle.General Access Type
TYPE V_Pointer IS ACCESS ALL MotorVehicle'Class; Can designate ( point to) a value of MotorVehicle or anything derived from it.Aliased Variable
V: ALIASED MotorVehicle; Can be pointed to by a value of type V_Pointer. VP: V_Pointer; VP := V'Access; VP contains a pointer to V.
NEW
or by a copy of another access value; a general access type
can acquire a value either by a NEW
call or by taking the
'Access
attribute of an aliased variable.Supplies
. For each
supply, the cost, number on hand, and reorder point must be stored. For
Paper
, the information needed is the number of sheets per box and
the size of the paper. For Ribbon
, the size, color, and kind
(Carbon
or Cloth
) are needed. For
Labels
, the size and number per box are needed. Use whatever data
types are appropriate for each field.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.