home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP15.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  13.6 KB  |  295 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 15
  5.                                                   VIRTUAL METHODS
  6.  
  7.  
  8.  
  9. Since we covered encapsulation and inheritance in the last chapter,
  10. we are left with only virtual methods to complete the major topics
  11. of object oriented programming.  Virtual methods, as they are
  12. called in TURBO Pascal, have several other names in the literature
  13. to describe the same technique.  This technique is sometimes called
  14. run-time binding or late binding referring to when the decision is
  15. made as to what method will respond to the message.  The use of
  16. virtual methods moves the responsibility of selection from the
  17. client (the logic sending the message) to the supplier (the methods
  18. responding to the message).  We will begin with a skeleton of a
  19. program without a virtual method and add one to show the effect of
  20. adding a virtual method.
  21.  
  22.  
  23.  
  24. WITHOUT A VIRTUAL METHOD
  25. _________________________________________________________________
  26.  
  27. The example program named VIRTUAL1.PAS will be     ==============
  28. used as the starting point for the study of         VIRTUAL1.PAS
  29. virtual methods.  We must state that this program  ==============
  30. does not contain a virtual method, it is only the
  31. starting point for studying them.
  32.  
  33. The objects included here are very similar to the objects
  34. describing vehicles which we were working with in the last chapter. 
  35. You will notice that all three objects contain a method named
  36. Message in lines 11, 20, and 31.  The Message method will be the
  37. center of our study in the first three example programs.  It should
  38. be pointed out that the constructors for the three objects are
  39. called in lines 98 through 100 even though a constructor call is
  40. still not absolutely necessary in this case.  We will have more to
  41. say about the constructor calls during the next example program.
  42.  
  43. Compile and execute the program and you will find that even though
  44. it is legal to pass the objects of type Car and Truck to the method
  45. named Output_A_Message in lines 109 and 110, the method that is
  46. called from line 86 is the method named Message in the parent type
  47. Vehicle.  This is probably no surprise to you since we defined an
  48. object of type Vehicle as a formal parameter of the method
  49. Output_A_Message.  We need only one small change and we will have
  50. a virtual procedure call.
  51.  
  52. Even though this program seems to do very little, it will be the
  53. basis of our study of virtual methods so you should study the code
  54. in detail.
  55.  
  56.  
  57.  
  58.                                                         Page 15-1
  59.  
  60.                                      Chapter 15 - Virtual Methods
  61.  
  62. NOW TO MAKE IT A VIRTUAL METHOD
  63. _________________________________________________________________
  64.  
  65. Examine the example program named VIRTUAL2.PAS,  ================
  66. and you will find only one small change in the     VIRTUAL2.PAS
  67. code but a world of difference in the way it     ================
  68. executes.
  69.  
  70. The careful student will notice the addition of the reserved word
  71. virtual in lines 13, 22, and 33.  This makes the method named
  72. Message a virtual method which operates a little differently from
  73. the way it did in the last program.  Once again, we call the three
  74. constructors in lines 100 through 102 and this time the constructor
  75. calls are absolutely essential.  We will discuss why in a couple
  76. of paragraphs.
  77.  
  78. Once again we send a message to Output_A_Message three times in
  79. lines 110 through 112 and line 88 is used to send a message to the
  80. Message method.  When we compile and execute this program, we find
  81. that even though the method Output_A_Message only uses the parent
  82. type Vehicle, the system calls the correct procedure based on the
  83. type of the actual object passed to this method.  The system sends
  84. a message to the objects of the correct type instead of to the
  85. parent type as may be expected.  It should be clear to you that the
  86. object that is to receive the message is not known at compile time
  87. but must be selected at run time when the object arrives at the
  88. method Output_A_Message.  This is known as late binding since the
  89. type is not known until run time as opposed to early binding where
  90. the type is known at compile time.  Every subprogram call in this
  91. entire tutorial, up to this point, has been early binding.
  92.  
  93. You will note that even though the method Output_A_Message only
  94. knows about the objects of type Vehicle, it has the ability to pass
  95. through other types, provided of course that they are descendant
  96. types of Vehicle.  The method Output_A_Message only passes the
  97. message through, it does not do the selection.  The selection is
  98. done by the objects themselves which answer the messages passed to
  99. them.  This means that the sender does not know where the message
  100. will be answered from, and it is up to the receiver to find that
  101. a message is being sent its way and to respond to it.  It is often
  102. said that the supplier (the method doing the work) must make the
  103. decision to answer the message, rather than the client (the user
  104. of the work done).  The burden is placed on the supplier to do the
  105. right thing.
  106.  
  107. If a method is declared virtual, all methods of that name must also
  108. be virtual including all ancestors and all descendants.  It is not
  109. possible to declare part of the methods of the same name virtual
  110. and part standard.  All parameter lists for all virtual methods of
  111. the same name must also be identical since they must all be capable
  112. of being called by the same method call.
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 15-2
  118.  
  119.                                      Chapter 15 - Virtual Methods
  120.  
  121. ASSIGNING DESCENDANTS TO ANCESTORS?
  122. _________________________________________________________________
  123.  
  124. It is legal in any object oriented language to assign a descendant
  125. object to an ancestor variable but the reverse is not true.  A
  126. vehicle, for example, can be used to define a car, a truck, a bus,
  127. or any number of other kinds of vehicles so it can be assigned any
  128. of those values.  A car on the other hand, is too specific to be
  129. used for the definition of anything but a car, so it cannot have
  130. any other value assigned to it.  A vehicle is very general and can
  131. cover a wide range of values, but a car is very specific and can
  132. therefore only define a car.
  133.  
  134.  
  135. WHY USE A CONSTRUCTOR?
  136. _________________________________________________________________
  137.  
  138. The constructor is absolutely required in this case because of the
  139. way the authors of TURBO Pascal defined the use of virtual
  140. functions.  The constructor sets up a pointer to a virtual method
  141. table (VMT) which is used to find the virtual methods.  If there
  142. is no pointer, the system jumps off to some unknown location and
  143. tries to execute whatever happens to be there and could do almost
  144. anything at that unknown and undefined point in the code.  So it
  145. is important to call a constructor once for each object as is done
  146. here so the pointer to the VMT can be initialized to the proper
  147. value.  If you make several objects of one type, it is not enough
  148. to call a constructor for one object and copy that object into each
  149. of the other objects.  Each object must have its own constructor
  150. call in order to prevent a system crash.
  151.  
  152. The strange looking code in line 6 tells the system to check each
  153. call to a virtual function to see if the constructor has been
  154. called.  This slows the program down slightly but will result in
  155. an error message if a virtual method is called prior to its VMT
  156. being properly set up with a constructor call.  After a program is
  157. thoroughly tested, the code can be removed from line 6 to speed up
  158. the program slightly by eliminating the checks.  Be warned however,
  159. that a call to a virtual method without A VMT will probably result
  160. in the computer hanging up.
  161.  
  162.  
  163. VIRTUALS AND POINTERS
  164. _________________________________________________________________
  165.  
  166. The example program named VIRTUAL3.PAS is nearly   ==============
  167. identical to the last program except that this      VIRTUAL3.PAS
  168. program uses pointers to objects instead of using  ==============
  169. the objects directly.
  170.  
  171. You will notice that once again, the methods named Message are all
  172. defined as virtual and a pointer type is defined for each object
  173. type.  In lines 99 through 101, three pointers are declared and
  174. memory is dynamically allocated on the heap for the objects
  175.  
  176.                                                         Page 15-3
  177.  
  178.                                      Chapter 15 - Virtual Methods
  179. themselves.  The objects are all sent a constructor message to
  180. initialize the stored data within the objects and to set up the VMT
  181. for each.  The rest of the program is nearly identical to the last
  182. program except that Dispose procedures are called for each of the
  183. dynamically allocated objects.  The code used in line 6 of the last
  184. program to force a check of each virtual method call has been
  185. removed to illustrate that it doesn't have to be there if you are
  186. sure a message is sent to a constructor once for each object with
  187. a virtual method.
  188.  
  189. Compiling and executing this program will give the same result as
  190. the last program indicating that it is perfectly legal to use
  191. pointers to objects as well as the objects themselves.
  192.  
  193.  
  194. AN ANCESTOR OBJECT
  195. _________________________________________________________________
  196.  
  197. The example program PERSON.PAS is not a complete   ==============
  198. program at all but only an object definition         PERSON.PAS
  199. within a unit.  This unit should pose no problem   ==============
  200. for you to understand so we will not say much
  201. except to point out that the method named Display
  202. is a virtual method.
  203.  
  204. This example program, as well as the next two example programs,
  205. have been carefully selected to illustrate the proper way to
  206. package objects for use in a clear understandable manner.
  207.  
  208. Compile this unit to disk in order to make it available for use in
  209. the remainder of this chapter.
  210.  
  211.  
  212. SOME DESCENDENT OBJECTS
  213. _________________________________________________________________
  214.  
  215. The example program named SUPERVSR.PAS is another  ==============
  216. unit which contains three descendants of the        SUPERVSR.PAS
  217. previously defined object named Person_ID.  You    ==============
  218. will notice that each of the objects have a method
  219. named Display which is virtual just as the same
  220. method in the ancestor object was.
  221.  
  222. The interface for each object has been purposely kept very simple
  223. in order to illustrate the use of objects.  The implementation has
  224. also been kept as simple as possible for the same reason so the
  225. diligent student should have no trouble in understanding this unit
  226. completely.
  227.  
  228. Once again, be sure to compile this unit to disk in order to make
  229. it available for use in the next few example programs. 
  230.  
  231.  
  232.  
  233.  
  234.  
  235.                                                         Page 15-4
  236.  
  237.                                      Chapter 15 - Virtual Methods
  238.  
  239. A COMPLETE EMPLOYEE PROGRAM
  240. _________________________________________________________________
  241.  
  242. Although the program named EMPLOYEE.PAS is a very  ==============
  243. short program that does very little, it is a        EMPLOYEE.PAS
  244. complete program to handle a very small amount of  ==============
  245. data about your employees.
  246.  
  247. You will notice that we declare an array of ten pointers to the
  248. Person_ID object and one pointer to each of the three descendant
  249. objects.  In the main program we send a message to the constructor
  250. for each of the array elements.  Inspection of the Person_ID.Init
  251. code will reveal that this initialization does nothing.  It is used
  252. to initialize the pointer to the VMT for each object, so the
  253. message must be sent.  We then dynamically allocate six objects of
  254. assorted descendant objects being careful to send a message to the
  255. constructor for each object.  This is done to generate a VMT for
  256. each object as it is allocated.  Finally, we send a message to the
  257. first six objects pointed to by the array of pointers instructing
  258. them to display their values.
  259.  
  260. When the program is compiled and executed, we find that the virtual
  261. methods were called as explained in the last example program.  Even
  262. though only one kind of pointer was passed to the Display method,
  263. three different messages were actually displayed, each message
  264. being of the proper kind based on the type of pointer used.
  265.  
  266. You will notice how clean and neat the main program is.  It is
  267. extremely easy to follow because all of the implementation details
  268. have been moved to the objects themselves.  Once the objects are
  269. carefully defined and debugged, the main program is usually a snap
  270. to write and debug.
  271.  
  272. Object oriented programming requires a whole new mindset over the
  273. procedural methods you have been using but after you catch on to
  274. the technique, you will find your programs much easier to debug and
  275. maintain.  The one thing you should avoid is the use of too many
  276. objects in your first program.  It is best to define a few simple
  277. objects for your first attempt at object oriented programming and
  278. write the rest of the program using standard procedural methods. 
  279. Then as you gain experience, you can begin using more and more
  280. objects until you finally write a program that is essentially all
  281. objects.  Of course, you will find that you will always write at
  282. least part of your program in a standard procedural format as was
  283. done in EMPLOYEE.PAS in this chapter.
  284.  
  285. PROGRAMMING EXERCISES
  286. _________________________________________________________________
  287.  
  288. 1.   Add a new object type to SUPERVSR.PAS to define a Consultant
  289.      defining appropriate data fields for him, then add a couple
  290.      of Consultant type objects to EMPLOYEE.PAS to use the new
  291.      object type.
  292.  
  293.  
  294.                                                         Page 15-5
  295.