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.
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.
+
, -
, *
, /, **
,
MOD
, REM
, ABS
, AND
,
OR
, NOT
, XOR
, &
,
<
, <=
, >
, and
>=
all can be overloaded; =
, /=
,
IN
, and NOT IN
cannot be.PRIVATE
types. Which operations can be
done on objects of a PRIVATE
type?
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.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.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.