Previous | Next | Table of Contents | Index | Program List | Copyright

Chapter Review

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.

New Ada Constructs in Chapter 15

The new Ada constructs introduced in this chapter are described in Table 15.1.

Table 15.1
Summary of New Ada Constructs

Construct				Effect
Tagged 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;			Automobile

Extension 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.

Quick-Check Exercises

  1. What is a tagged type? How is it related to a variant record?
  2. What is a primitive operation?
  3. What is a derived tagged type?
  4. What is the difference between a pool-specific and a general access type?
  5. What is an aliased variable?

Answers to Quick-Check Exercises

  1. A type that is extensible, that is, one from which new types can be derived that have additional fields.
  2. An operation on a tagged type that is declared just below it in the same package specification and has a parameter of the tagged type.
  3. A type that has been derived from a previously-defined tagged type, possibly with an extension consisting of one or more new fields.
  4. A pool-specific access type can acquire a value only by a call to 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.

Review Questions for Chapter 15

  1. Write the tagged type declarations for 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.
  2. For the motor vehicle example begun in Table 15.1, complete a reasonably full set of tagged type declarations that describe different types of vehicles, epecially motorcycles, automobiles, and large trucks.

Programming Projects

  1. Modify the geometric shapes example from Section 12.5, so that the basic shape is a tagged type with perimeter and area fields, and other shapes are derived from the basic one. Use the style of the employees example from Section 15.2.
  2. Modify the employees and data base packages (Programs 10.11 through 10.18) to accommodate the tagged record hierarchy described in this chapter.
  3. Develop a data base structure for motor vehicles, similar to the employees data base of Programs 10.11 through 10.18, starting with the tagged types in Table 15.1 and adding a realistic set of derived types.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.