home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap07.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  16.3 KB  |  413 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 7
  5.                                                     DERIVED TYPES
  6.  
  7.  
  8. LET'S TALK ABOUT TYPES
  9. _________________________________________________________________
  10.  
  11. We have been talking about types throughout this tutorial, but
  12. we've been sort of working our way around them rather than
  13. carefully defining the various kinds of types available in Ada.
  14. This chapter will be devoted to a full discussion of types.  It was
  15. not possible to discuss types in full until we covered a bit of
  16. material about the various scalar types and how they are used, but
  17. with that material behind us, we will give a full treatment to the
  18. topic of types.
  19.  
  20. There are four different ways to declare a type.  You can probably
  21. guess that we will discuss each of the ways in detail in this
  22. chapter.  They are listed as follows;
  23.  
  24. 1.   Predefined types.  These are provided for us by the compiler
  25.      writer as defined in the LRM.  We have already used several
  26.      of these.
  27.  
  28. 2.   User defined types.  We have already defined a few new types
  29.      in some of the earlier programs.
  30.  
  31. 3.   Derived types.  These get their name because they are defined
  32.      in part based on a previously defined type and derive some of
  33.      their characteristics from those types.  We have not yet
  34.      encountered the derived type.
  35.  
  36. 4.   Subtypes.  These are usually a subset of another type upon
  37.      which they are based.  We encountered subtypes in the last
  38.      chapter.
  39.  
  40. Before we launch into a detailed definition of these four type
  41. definition methods, we must devote a little attention to the
  42. various type classes.
  43.  
  44.  
  45.  
  46. TYPE CLASSES IN ADA
  47. _________________________________________________________________
  48.  
  49. There are five type classes in Ada, a class being a rough grouping
  50. of types because of similar characteristics.  The five type classes
  51. and a brief definition of each is given below.
  52.  
  53. Scalar class - This includes all of the types we have used so far
  54.        in this tutorial, and are characterized by the fact that each
  55.        variable of this class can store a single number, either
  56.        variable or constant.  Discrete types are a subset of this
  57.  
  58.                                                          Page 7-1
  59.  
  60.                                         Chapter 7 - Derived Types
  61.  
  62.        class and include all integer types, enumerated types, and
  63.        the CHARACTER type.  All real types are included in the scalar
  64.        class, but not the discrete subclass.  The remainder of this
  65.        chapter will utilize scalar class variables as examples of the
  66.        various type definition methods.
  67.  
  68. Composite class - This class includes the array types and the
  69.        record types.
  70.  
  71. Access class - Variables of this type are similar to the pointer
  72.        type in Pascal or C, and are used to access a variable or
  73.        group of variables in an indirect manner.
  74.  
  75. Private class - This class of types is used for information hiding
  76.        techniques when used on a multi-programmer project.
  77.  
  78. Tasking class - This type class is used for concurrent processing.
  79.  
  80. All of these classes will be illustrated in detail later in this
  81. tutorial.  The concept of typing is one of the most important in
  82. Ada, because the proper use of typing will act as a tremendous aid
  83. in locating programming errors.  Keep in mind that all types are
  84. static and cannot be changed during program execution.
  85.  
  86.  
  87.  
  88. PREDEFINED TYPES IN ADA
  89. _________________________________________________________________
  90.  
  91. The LRM requires every Ada compiler to provide several predefined
  92. types, including INTEGER, NATURAL, POSITIVE, FLOAT, CHARACTER, and
  93. STRING.  Several other types are optional and include LONG_INTEGER,
  94. SHORT_INTEGER, LONG_FLOAT, and SHORT_FLOAT.  It will be left to you
  95. to study the documentation that came with your compiler and see
  96. which of the optional types are included with your compiler
  97. package.
  98.  
  99. The predefined types can be used as the basis of the declaration
  100. of new types for your specific application and is the topic of this
  101. chapter.
  102.  
  103.  
  104.  
  105. USER DEFINED TYPES
  106. _________________________________________________________________
  107.  
  108. Examine the program named DERTYPES.ADA for       ================
  109. several examples of user defined types.  Lines     DERTYPES.ADA
  110. 10 through 12 each declare an entirely new type. ================
  111. Considering the predefined type INTEGER and
  112. these three, we actually have four types to use
  113. in the program, and each type has nothing to do with the other
  114. three.  Variables of these types cannot be intermixed in any way
  115. without the proper type conversion explicitly stated by the
  116.  
  117.                                                          Page 7-2
  118.  
  119.                                         Chapter 7 - Derived Types
  120.  
  121. programmer.  Moreover, variables declared with one of these types
  122. cannot be assigned a value that is outside of its declared range.
  123. The structure for declaring a user defined integer class type is
  124. given as,
  125.  
  126.      type <type-name> is range <lower-limit>..<upper-limit>;
  127.  
  128. As stated earlier, the word range is a reserved word, and its
  129. presence indicates to the compiler that you wish to have a type of
  130. the integer class.
  131.  
  132. Use of the three new types is not illustrated in this example
  133. program, but the diligent student will have no trouble using these
  134. new types to declare a few variables, then use them in some
  135. mathematical statements.
  136.  
  137.  
  138.  
  139. DERIVED TYPES ARE RELATIVELY NEW
  140. _________________________________________________________________
  141.  
  142. Derived types are an entirely new subject, since they are not
  143. available in any of the more popular languages that you may have
  144. been programming in.  Derived types get their name because they are
  145. derived from an existing type rather than being an entirely new
  146. creation.  A derived type has all of the operations available that
  147. are available with the parent type but it may have a more limited
  148. range than the range of the parent type.  The example program has
  149. as illustration of this in line 13 where the type TINY_POS is
  150. derived from the user defined type POS_INT but with a slightly
  151. tighter allowable range.
  152.  
  153. Any operation that is legal to be performed on a variable of type
  154. POS_INT is legal for a variable of type TINY_POS.  In fact, if we
  155. would have declared a subprogram (which we haven't studied yet),
  156. prior to line 13, that operated on variables of type POS_INT, then
  157. that same subprogram could be used for variables of type TINY_INT.
  158. We will have more to say about this when we get to the chapter on
  159. subprograms in this tutorial.
  160.  
  161. The key to a derived type is the use of the reserved word new along
  162. with the type from which the new type will be derived.  The
  163. structure for declaring a user defined derived type is given as,
  164.  
  165.      type <type-name> is new <existing-type> <optional-range>;
  166.  
  167. The derived type will be of the same class as the type from which
  168. it is derived, in this case it will be of the integer type class.
  169.  
  170. We continue declaring new types in lines 14 through 16 and because
  171. these three are entirely new types, each derived from the parent
  172. type INTEGER, they cannot be added together, subtracted, compared
  173. or even assigned to each other without a bit of extra trouble.
  174. Each of the seven new types in lines 10 through 16 share all of
  175.  
  176.                                                          Page 7-3
  177.  
  178.                                         Chapter 7 - Derived Types
  179.  
  180. the operations that are defined for INTEGER, including the
  181. arithmetic, logical, and assignment operations, but the operations
  182. can only be performed as long as the types of the objects are
  183. consistent.  Of course, the explicit type conversion can be done
  184. to allow the combination of variables of any of these types.
  185.  
  186. The type TREE_INT, defined in line 16, is a derived type with a
  187. more limited range than the parent type so it has all of the
  188. characteristics of the parent type except that it cannot be
  189. assigned a value outside of its defined range.
  190.  
  191.  
  192.  
  193. HOW TO USE SOME OF THE NEW TYPES
  194. _________________________________________________________________
  195.  
  196. As an example of using the new types, consider the type SALAD_INT
  197. which is a derived type of the parent type INTEGER.  Since three
  198. variables, Salad, Lettuce, and Tomatoes, are all of the same type,
  199. they can be freely added, compared, assigned to each other, or used
  200. in any way legal for integer class variables.  They can be used as
  201. the iteration index or range limits in a for loop.  The three
  202. variables have the same range as the parent type INTEGER, namely
  203. -32768 to 32767 on most 16 bit machines.  Constants of type
  204. universal_integer can be added to, compared to, or assigned to
  205. these three variables, as well as any of the other variables
  206. declared in this program.
  207.  
  208.  
  209.  
  210. HOW CAN THESE NEW TYPES HELP IN A PROGRAM?
  211. _________________________________________________________________
  212.  
  213. Everything that was said about type SALAD_INT in the last paragraph
  214. is true of type ANIMAL_INT, TREE_INT, or any of the other four
  215. types.  Suppose somewhere in our program we tried to add the number
  216. of Tomatoes to the number of Dogs and assign the result to Trees.
  217. If the variable names are meaningful, we would probably not want
  218. to do such an operation in any practical program.  The Ada compiler
  219. would give us a compile time error, so we would detect the error
  220. before we tried to run the program with such a silly statement.
  221. Careful assignment of types can be used to protect us from the
  222. silly little errors that we are all so prone to make.  It would be
  223. much more efficient to let the compiler find these silly little
  224. errors and free us up to find the analysis errors we also make.
  225.  
  226.  
  227.  
  228. HOW DO YOU DO A TYPE TRANSFORMATION?
  229. _________________________________________________________________
  230.  
  231. Even though you set things up very carefully, you may need to
  232. perform some operations on the data where you actually do need to
  233. add the number of Animals to the total of Oak plus Coconut.  Line
  234.  
  235.                                                          Page 7-4
  236.  
  237.                                         Chapter 7 - Derived Types
  238.  
  239. 33 illustrates how to do this.  Enclosing the variable in
  240. parentheses and adding the desired type to the front of the
  241. grouping will change the type from the variable's actual type to
  242. the type in front of the parentheses, but only for that one place
  243. in the program.  If you want to use the type transformation again,
  244. you add the type in front of the variable again.
  245.  
  246. In line 34, Trees and Salad are both transformed to type INTEGER
  247. before being summed and assigned to Count, a variable of type
  248. INTEGER.  Line 36 illustrates the addition of many variables by
  249. first transforming each to type SALAD_INT then performing the
  250. addition.  In line 43, the same variables are added together, but
  251. in this case, all variables are transformed into type ANIMAL_INT
  252. prior to summing, then the sum is transformed to type SALAD_INT.
  253. The two methods should result in the same answer, and you can
  254. verify that they do when you compile and run the program.
  255.  
  256.  
  257.  
  258. WHAT IS A SUBTYPE?
  259. _________________________________________________________________
  260.  
  261. A subtype is a new type based on a parent type but usually with a
  262. more restricted range.  It inherits all of the characteristics of
  263. the parent type and in addition, it can be freely intermixed with
  264. the parent type in calculations and assignment statements.  The
  265. reason for using a subtype is usually to declare a variable of the
  266. parent type but with a more limited range to take advantage of the
  267. range checking capability of Ada.
  268.  
  269.  
  270.  
  271. WE CAN ALSO HAVE SUBTYPES OF DERIVED TYPES
  272. _________________________________________________________________
  273.  
  274. The program named DERSUBS.ADA gives an example    ===============
  275. of the definition and use of a subtype of a         DERSUBS.ADA
  276. derived type, and a derived type of a subtype.    ===============
  277. In line 10 we declare a derived type and in line
  278. 13 we declare a subtype of the new derived type.
  279. The subtype of the derived type has the same characteristics as
  280. the derived type except that it has a more restricted range in this
  281. case.  Variables of type NEW_SUBTYPE are compatible with variables
  282. of their parent type NEW_INT.  This is illustrated in line 29.
  283.  
  284. In this case, NEW_SUBTYPE is as different from the type INTEGER,
  285. as SALAD_INT was in the last program.
  286.  
  287.  
  288. WE CAN HAVE A DERIVED TYPE OF A SUBTYPE
  289. _________________________________________________________________
  290.  
  291. Line 15 illustrates the declaration of a derived type based on
  292. using a subtype for the parent type.  Note that the new derived
  293.  
  294.                                                          Page 7-5
  295.  
  296.                                         Chapter 7 - Derived Types
  297.  
  298. type has all of the characteristics of its parent type except for
  299. the more restricted range, but once again, it is an entirely new
  300. type as far as type checking is concerned.
  301.  
  302.  
  303.  
  304. A SUBTYPE CAN BE SIMPLY A SYNONYM
  305. _________________________________________________________________
  306.  
  307. Line 13 illustrates a subtype which covers the entire range of its
  308. parent type.  Since variables of this subtype can be freely
  309. intermixed with variables of its parent type, the subtype name is
  310. simply a synonym for the parent type name.
  311.  
  312. With the discussion of the last program fresh in your mind, you
  313. should breeze through the remainder of this program.  Be sure to
  314. compile and execute it.
  315.  
  316.  
  317.  
  318. USING OTHER PREDEFINED TYPES FOR THE PARENT
  319. _________________________________________________________________
  320.  
  321. Examine the program named MOREDERS.ADA for       ================
  322. examples of derived types and subtypes based on    MOREDERS.ADA
  323. some of the other predefined types in Ada.  We   ================
  324. begin by declaring two user defined types in
  325. lines 8 and 9 which are of the floating point
  326. class of types because of the reserved word digits appearing in the
  327. definition.  In line 10 we declare DER_FLOAT which has all of the
  328. characteristics of the predefined type FLOAT, except that the
  329. compiler will consider it to be an entirely different type and will
  330. not allow mixing of these two types.  Of course, type conversion
  331. can be used if necessary.
  332.  
  333. The derived type LIM_FLOAT is declared with all the characteristics
  334. of FLOAT except that it has a limited range to allow for compiler
  335. checks.  Line 12 contains the definition of a subtype based on
  336. DER_FLOAT with a more limited range.  Variables of the type
  337. SUB_FLOAT can be freely intermixed with variables of type
  338. DER_FLOAT, but not with those declared with the types FLOAT,
  339. NEW_FLOAT1, NEW_FLOAT2, or LIM_FLOAT.
  340.  
  341. Lines 15 through 19 illustrate the same principles applied to fixed
  342. point types and should be self explanatory.  The only difference
  343. is the use of the reserved word delta in the fixed point
  344. definitions.
  345.  
  346. Lines 22 through 29 illustrate the declaration of derived types and
  347. subtypes of the CHARACTER type and an enumerated type.  These
  348. statements will be left to the students study since they are so
  349. similar to the example using FLOAT as the parent type.
  350.  
  351.  
  352.  
  353.                                                          Page 7-6
  354.  
  355.                                         Chapter 7 - Derived Types
  356.  
  357. A few variables are declared and some are initialized in lines 32
  358. through 35, and a nonsense calculation is given in line 39 to
  359. illustrate the type transformations that can be done with derived
  360. types.
  361.  
  362. Be sure to compile and execute this program even though it has no
  363. output.
  364.  
  365.  
  366.  
  367. A WORD OF SUMMARY ABOUT TYPES
  368. _________________________________________________________________
  369.  
  370. We have seen that in addition to the predefined types, we can
  371. declare additional types for use in our programs.  We can then use
  372. any of the predefined or user defined types as the parent type for
  373. either subtypes or derived types.  The new subtype or derived type
  374. can be used as a parent type for additional subtypes or derived
  375. types and we find that we have a tremendous amount of flexibility
  376. in defining the data to solve any particular problem.
  377.  
  378.  
  379.  
  380. PROGRAMMING EXERCISE
  381. _________________________________________________________________
  382.  
  383. 1.   Modify the program named DERTYPES.ADA to include a new
  384.      instantiation of the package Text_IO.Integer_IO to output the
  385.      variable named Salad in line 40 and 47 without the type
  386.      conversion.
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.                                                         Page 7-7
  412.  
  413.