正在加载图片...
$33.3 A STACK IMPLEMENTATION 1083 the need for an explicit initialization operation corresponding to creation;this would not be the case,however,if a less straightforward initialization were required A few details of Ada are needed to understand the type declarations:POS/T/IE and NATURAL denote the subtypes of INTEGER covering positive and non-negative integers,respectively;a type specification of the form array (TYPE range<>), where <is known as the Box symbol,describes a template for array types.To derive an actual type from such a template,you choose a finite subrange of TYPE; this is done here in STACK,which uses the subrange 1..capacity of POS/TIVE. STACK is an example of a parameterized type;any declaration of an entity of type STACK must specify an actual value for capacin,as in s:STACK (1000) In Ada,every routine argument must be characterized by a mode:in,out or in out, defining the routine's rights on the corresponding actual arguments (read-only, write-only or update).In the absence of an explicit keyword,the default mode is in. Finally,the interface also specifies two exception names:Overflow and Underflow An exception is an error condition that the programmer has decided to treat separately from the normal flow of control.The interface of the package should list any exceptions that may be raised by the package's routines and propagated to clients.More on the Ada exception mechanism below. Using a package Client code using the package is based on the interface.Here is an example from some package needing a stack of real numbers: s:REAL STACKS.STACK (1000): REAL STACKS.put (3.5,s);...; if REAL STACKS.empty (s)then ... An Ada environment must be able to compile such client code even if only the interface of REAL STACKS,not its body,is available. Syntactically,note how each use of an entity from this package (where "entities" here include type names such as STA4CK as well as routine names)must repeat the name of package REAL STACKS,using dot notation.This could become tedious,hence the need for a more implicit form of qualification.If you include the directive use REAL STACKS: at the beginning of the client package,you may write the above extract more simply as s:STACK (1000). pt(3.5,s if empry (s)then ...§33.3 A STACK IMPLEMENTATION 1083 the need for an explicit initialization operation corresponding to creation; this would not be the case, however, if a less straightforward initialization were required. • A few details of Ada are needed to understand the type declarations: POSITIVE and NATURAL denote the subtypes of INTEGER covering positive and non-negative integers, respectively; a type specification of the form array (TYPE range < >), where <> is known as the Box symbol, describes a template for array types. To derive an actual type from such a template, you choose a finite subrange of TYPE; this is done here in STACK, which uses the subrange 1..capacity of POSITIVE. STACK is an example of a parameterized type; any declaration of an entity of type STACK must specify an actual value for capacity, as in s: STACK (1000) • In Ada, every routine argument must be characterized by a mode: in, out or in out, defining the routine’s rights on the corresponding actual arguments (read-only, write-only or update). In the absence of an explicit keyword, the default mode is in. • Finally, the interface also specifies two exception names: Overflow and Underflow. An exception is an error condition that the programmer has decided to treat separately from the normal flow of control. The interface of the package should list any exceptions that may be raised by the package’s routines and propagated to clients. More on the Ada exception mechanism below. Using a package Client code using the package is based on the interface. Here is an example from some package needing a stack of real numbers: s: REAL_STACKS ● STACK (1000); REAL_STACKS ● put (3.5, s); …; if REAL_STACKS ● empty (s) then …; An Ada environment must be able to compile such client code even if only the interface of REAL_STACKS, not its body, is available. Syntactically, note how each use of an entity from this package (where “entities” here include type names such as STACK as well as routine names) must repeat the name of package REAL_STACKS, using dot notation. This could become tedious, hence the need for a more implicit form of qualification. If you include the directive use REAL_STACKS; at the beginning of the client package, you may write the above extract more simply as s: STACK (1000); put (3.5, s); …; if empty (s) then …;
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有