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

Chapter Review

In this chapter you studied abstract data types, or ADTs, implemented in Ada as packages. ADTs are characterized by a type and a set of operations applicable to that type. In Ada, the type in an ADT package is often declared as PRIVATE, which prevents a client program from directly accessing the values stored in variables of the type, requiring instead that the client use package-provided operations.

Operator overloading is another useful Ada feature introduced here. If the ADT is a mathematical type for which addition, for example, is appropriate, this addition operation can be called "+". Similarly, a comparison operation implementing "less than" for the new type can be called "<".

Yet another important concept used in this chapter is the package-provided exception. An exception can be defined to report an unusual condition, such as a client action that violates an assumption of the package. If an exception is provided in the package specification, a client program can handle it with a normal Ada exception handler. Exception handling is thus no different for package-provided exceptions than it is for predefined ones.

New Ada Constructs in Chapter 10

The new constructs introduced in this chapter are given in Table 10.1. Table 10.1
New Ada Constructs

Construct				Effect

Private Type Definition

PACKAGE ComplexNumbers IS

  TYPE Complex IS PRIVATE;		Defines a type Complex
					which has no predefined
	...				operations other than
					copying and equality.

PRIVATE

  TYPE Complex IS RECORD		The type definition is
    RealPart: Float;			completed here in the
    ImaginaryPart: Float;		PRIVATE section.
  END RECORD;

END ComplexNumbers;
User-Defined Exception
SomethingIsWrong: EXCEPTION;		Usually placed in a
					package specification;
					defines an exception
					that can be raised by
					an operation in the
					package body and handled
					by an exception handler
					in the client program.

Operator Overloading

FUNCTION "+"(Left, Right: Rational) RETURN Rational;	Creates an additional
							meaning for the
							operator.

Quick-Check Exercises

  1. A ___________ operation selects a particular component of an ADT object, a ___________ creates an ADT object from its component parts, and a ___________ operation asks whether an ADT object has a given property.
  2. The syntax for an exception handler depends on whether the exception is a predefined one or a user-defined one (True/False).
  3. List all the operator symbols in Ada that can be overloaded. List the ones that cannot.

Answers to Quick-Check Exercises

  1. Selector, constructor, inquiry
  2. False--the syntax is exactly the same.
  3. +, -, *, /, **, MOD, REM, ABS, AND, OR, NOT, XOR, &, <, <=, >, and >= all can be overloaded; =, /=, IN, and NOT IN cannot be.

Review Questions

  1. Explain the rules for PRIVATE types. Which operations can be done on objects of a PRIVATE type?
  2. Suppose we wrote, and included in Currency, an operation called "*" that actually added its operands instead of multiplying them. Would this be legal in Ada? Explain. Even if it is legal, give some reasons why it is not a good idea to do this.

Programming Projects

  1. The World_Time program presented in Section 10.2 has a limitation: The array of time-zone offsets must be completely redefined if the program user is not in the Eastern U.S. time zone. In many applications, time-zone offsets are computed with respect to Greenwich Mean Time, often referred to as GMT or Zulu. This is the local time in Greenwich, England. Modify World_Time so that Zulu is used as the "zero point" for the offsets. (Encyclopedias and almanacs usually describe the various official time zones around the world; so do amateur radio guides.) Because your computer's clock normally reports only local time, your program will need to find out from the user in which time zone he or she is located before it can compute the time elsewhere.
  2. Write a program that asks the user to enter a group of currency quantities from the keyboard, reads these numbers into an array, then sorts them and displays the largest, smallest, median, and average values.
  3. Complete and test the currency package of Section 10.4.
  4. Complete and test the various packages in the employee data base project and the interactive user interface.
  5. Refer to Section 8.4 on hierarchical records. Develop an ADT to handle address records. Given the ADTs for address records, employee records (Section 10.5), and dates (Section 10.3), develop an ADT that uses these to provide operations on the hierarchical employee records described in Section 8.4 and modify the inquiry system of Section 10.5 so that hierarchical employee records can be manipulated by the terminal user.


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

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