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

  1.  
  2.  
  3.  
  4.                                                        Chapter 12
  5.                                                           RECORDS
  6.  
  7.  
  8. OUR FIRST LOOK AT A RECORD
  9. _________________________________________________________________
  10.  
  11. Ada has provision for two composite types, the    ===============
  12. array, which we studied earlier, and the record,    RECORD1.ADA
  13. which is the topic of this chapter.  Examine the  ===============
  14. program named RECORD1.ADA for our first example
  15. of a record.
  16.  
  17. Lines 10 through 15 declare an Ada record, which actually only
  18. declares a type.  The usual syntax for a type is used but when we
  19. come to the type definition itself, we begin with the reserved word
  20. record.  The components of the record are then inserted, and the
  21. record type definition is terminated by the reserved words end
  22. record.
  23.  
  24. A record can contain any desired components, the only requirement
  25. being that the component types must be declared prior to this
  26. definition, and the record type cannot be included as a component
  27. of itself.  The key point to keep in mind about records is, whereas
  28. an array is composed of some number of like elements, the record
  29. is composed of some number of components that may be of different
  30. types.
  31.  
  32.  
  33. WHAT IS CONTAINED IN THE RECORD?
  34. _________________________________________________________________
  35.  
  36. It is impossible to declare an anonymous record type like you can
  37. do in Pascal.  The record must be a named type prior to being used
  38. in a variable declaration.
  39.  
  40. In this record, we have a variable named Month that is permitted
  41. to store any value from 1 through 12, obviously representing the
  42. months of the year.  There are also Day and Year variables, each
  43. of which is different from Month since different constraints are
  44. placed upon each.  After declaring the record type, we still have
  45. no actual variables, only a type, but in lines 17 through 19, we
  46. declare four variables of type DATE.  Since the variables are of
  47. type DATE, each has three components, namely a Month, Day, and
  48. Year.  Notice that two of the variables are initialized to the
  49. values given in parentheses in the order of the variable
  50. definitions.  Month is therefore set to 5, Day to 25, and Year to
  51. 1982 for each of the two initialized variables, Today and Pay_Day.
  52. The initialization will be very clear after we discuss the program
  53. itself, so we will come back to it later.
  54.  
  55.  
  56.  
  57.  
  58.                                                         Page 12-1
  59.  
  60.                                              Chapter 12 - Records
  61.  
  62. HOW DO WE USE THE RECORDS?
  63. _________________________________________________________________
  64.  
  65. Since Independence_Day is actually a variable composed of three
  66. different variables, we need a way to tell the computer which
  67. subfield we are interested in using.  We do this by combining the
  68. major variable name and the subfield with a dot as shown in lines
  69. 22 through 24.  This is called the selected component notation in
  70. Ada.  It should be clear to you that Independence_Day.Month is
  71. actually a single variable capable of storing an INTEGER type
  72. number as long as it is in the range of 1 through 12.  The three
  73. elements of the record are three simple variables that can be used
  74. in a program wherever it is possible to use any other integer type
  75. variable.  The three are grouped together for our convenience
  76. because they define a date which we call Independence_Day.  The
  77. data could be stored in three simple variables and we could keep
  78. track of them in the way we usually handle data, but the record
  79. allows a more convenient grouping and a few additional operations.
  80.  
  81.  
  82. THE RECORD ASSIGNMENT
  83. _________________________________________________________________
  84.  
  85. There is one big advantage to using a record and it is illustrated
  86. in line 26 where all three values associated with the variable
  87. Independence_Day are assigned to the three corresponding components
  88. of the variable Birth_Day.  If they were separate variables, they
  89. would have to be copied one at a time.  The Day field of Pay_Day
  90. is assigned a new value in line 28 and the date contained in
  91. Independence_Day is displayed on the monitor for illustrative
  92. purposes.
  93.  
  94.  
  95. NAMED AND POSITIONAL AGGREGATES
  96. _________________________________________________________________
  97.  
  98. Line 38 has an example of assignment using a named aggregate in
  99. which the three fields are defined with their respective names and
  100. the pointing operator.  It can be read as, "the variable named Day
  101. gets the value of 19, Month gets the value of 2, and so on".  Since
  102. they are named, they are not required to be in the same order that
  103. they are in the record definition, but can be in any order.  The
  104. real advantage to using the named aggregate notation is the fact
  105. that all elements are named and it is clear just what value is
  106. being assigned to each variable field.  It should be pointed out
  107. that an aggregate is a group of data which may or may not be of the
  108. same type.
  109.  
  110. Line 39 defines the three values of the record by simply giving the
  111. three values, but in this case, the three elements must be in the
  112. correct order so the compiler will be able to assign them to their
  113. correct subfields.  This is called a positional aggregate.  This
  114. is the kind of aggregate used to initialize the dates in line 19.
  115.  
  116.  
  117.                                                         Page 12-2
  118.  
  119.                                              Chapter 12 - Records
  120.  
  121. A MIXED AGGREGATE
  122. _________________________________________________________________
  123.  
  124. Line 40 illustrates use of a mixed aggregate in which some are
  125. defined by their position, and the rest are defined by their names.
  126. The positional definitions must come first, and after a named
  127. variable is given, the remainder must be named also.  One point
  128. that must be remembered, all values must be mentioned, even if some
  129. of them will not be changed.  This seems like a picky nuisance, but
  130. it greatly simplifies the compiler writer's job.
  131.  
  132. Compile and run this program, and you will get the date of
  133. Independence Day displayed on your monitor.  Be sure you understand
  134. this program, because understanding the next program requires that
  135. you thoroughly understand this one.  It should be clear that
  136. whether you use the named, positional, or mixed notation, you are
  137. required to use the correct types for each of the parameters.
  138.  
  139.  
  140. A RECORD CONTAINING A RECORD
  141. _________________________________________________________________
  142.  
  143. Examine the file named RECORD2.ADA for an         ===============
  144. example of a record declaration containing          RECORD2.ADA
  145. another record within it.  We declare the record  ===============
  146. type DATE in exactly the same manner that we did
  147. in the last program, but we go on to declare
  148. another record type named PERSON.  You will note that the new
  149. record is composed of four variables, one of the variables being
  150. of type DATE which contains three variables itself.  We have thus
  151. declared a record that contains three simple variables and a
  152. variable record containing three more variables, leading to a total
  153. of six separate variables within this one record type.  In line 22,
  154. we declare three variables, each composed of six simple variables,
  155. so we have 18 declared variables to work with in our example
  156. program.
  157.  
  158.  
  159. HOW TO USE THE COMPOSITE RECORD
  160. _________________________________________________________________
  161.  
  162. Lines 28 through 30 should pose no real problem for you since we
  163. are using knowledge gained during the last example program, but to
  164. assign the date requires another extension to our store of Ada
  165. knowledge.  Notice that in addition to the name of the main
  166. variable Self, we must mention the Birth_Day variable which is part
  167. of it, and finally the subfield of the Birth_Day variable, Month
  168. in line 31.  The variable name is therefore composed of the three
  169. names, "dotted" together resulting in the name of a unique simple
  170. variable.  Once again, this is called the selected component
  171. notation.  Lines 31 through 33 assign the remaining three fields
  172. of the variable Self some meaningful data.  Line 36 assigns all six
  173. elements of the variable Self to the variable Mother in one simple
  174. statement.  Line 37 is used to assign the values of only the three
  175.  
  176.                                                         Page 12-3
  177.  
  178.                                              Chapter 12 - Records
  179.  
  180. components of Mother's Birth_Day to the three corresponding
  181. components of Father's Birth_Day.
  182.  
  183. Since each subfield is actually only a simple variable, each one
  184. can be used in computations as illustrated in line 38 where
  185. Mother's Birth_Day Month is assigned the value which is 4 less than
  186. Self's Birth_Day Month.  This is only done to illustrate that the
  187. simple variables can be used in any way you so desire, provided
  188. that you follow the rules of simple types.
  189.  
  190.  
  191. RENAMING A RECORD COMPONENT
  192. _________________________________________________________________
  193.  
  194. Line 24 illustrates how you can rename a component of a record in
  195. order to ease the problem of entering the dotted notation each time
  196. a field is used.  In this case the simple name My_Birth_Year is a
  197. synonym for the extended naming required with all three components.
  198. Once again it must be pointed out that this only affects the
  199. compilation since it is only an additional name which can be used
  200. to refer to the variable.  It must also be repeated that this
  201. facility should not be used except in those few case where it
  202. really adds to the program clarity.  Correct use of the new name
  203. is illustrated in line 34.
  204.  
  205.  
  206. RECORD ASSIGNMENT AND COMPARISON
  207. _________________________________________________________________
  208.  
  209. As illustrated in lines 36 and 41, entire records can be assigned
  210. to other records of the same type, and entire records of the same
  211. type can be compared for equality or inequality.  The records are
  212. equal only if every subfield of one record is equal to the
  213. corresponding subfield of the other.  The other comparison
  214. operators are not available in Ada for records.
  215.  
  216. Compile and run this program even though you will not get any
  217. output.  Add some output statements yourself to see if you can get
  218. some of the data out to the monitor.
  219.  
  220.  
  221.  
  222. AN ARRAY WITHIN A RECORD
  223. _________________________________________________________________
  224.  
  225. Examine the file named RECORD3.ADA and you will   ===============
  226. find an array type declared in line 17 which is     RECORD3.ADA
  227. then used in the record type PERSON.  The         ===============
  228. addition allows a variable of type PERSON to
  229. store four grades giving us a little additional
  230. flexibility over the last program.  The method of assigning data
  231. to the new fields are illustrated in lines 38 through 41 and should
  232. require no additional comment, because you are already versed on
  233. how to use arrays.  One rule must be mentioned here, you are not
  234.  
  235.                                                         Page 12-4
  236.  
  237.                                              Chapter 12 - Records
  238.  
  239. allowed to declare an array with an anonymous type within a record,
  240. it must be named.  Be sure to compile and run this program.
  241.  
  242.  
  243. AN ARRAY OF RECORDS
  244. _________________________________________________________________
  245.  
  246. Examine the file named RECORD4.ADA for an         ===============
  247. example of an array of records.  The types DATE     RECORD4.ADA
  248. and PERSON are declared in a manner similar to    ===============
  249. their declaration in RECORD2.ADA, but in line 26
  250. we declare an array of 35 variables, each of
  251. type PERSON, so each is composed of six variable fields.  In lines
  252. 44 through 50, we assign some nonsense data to each field of the
  253. 35 variables by using a loop.  Finally, we assign nonsense data to
  254. a few of the variables to illustrate how it can be done in lines
  255. 52 through 57.  You should have no problem understanding this
  256. program.
  257.  
  258. Note that we could have assigned data to one of the records, the
  259. first for instance, then used it in a loop to assign values to all
  260. of the others by using a record assignment such as,
  261. "Class_Member(Index) := Class_Member(1);", and looping from 2 to
  262. 35.  In a useful program, the data to be assigned will be coming
  263. from a file somewhere, as we would probably be filling a database.
  264. This is, in fact, the beginning of a very crude database.
  265.  
  266. Another new construct is illustrated in lines 27 and 28 where we
  267. initialize the variable named Standard to the aggregate given.
  268. Note that, like the unnested record requirement, a nested record
  269. must be initialized with an aggregate which includes all values.
  270. Compile and run this program to assure yourself that it really will
  271. compile and run.
  272.  
  273.  
  274. PROGRAMMING EXERCISES
  275. _________________________________________________________________
  276.  
  277. 1.   Rewrite RECORD4.ADA and change the initialization aggregate
  278.      for Standard from a positional aggregate to a named aggregate.
  279.  
  280. 2.   Rewrite RECORD1.ADA to utilize the enumerated type for the
  281.      month field as used in RECORD3.ADA.  Note that you will need
  282.      to instantiate a copy of  the package named Enumerated_IO to
  283.      display the month name.
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.                                                         Page 12-5