home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / text / chap08.txt < prev    next >
Encoding:
Text File  |  1994-05-15  |  16.9 KB  |  406 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 8
  5.                                                  MORE INHERITANCE
  6.  
  7. In the last chapter we developed a model using modes of 
  8. transportation to illustrate the concept of inheritance.  In this 
  9. chapter we will use that model to illustrate some of the finer 
  10. points of inheritance and what it can be used for.  If it has 
  11. been a while since you read and studied chapter 7, it would be 
  12. good for you to return to that material and review it in 
  13. preparation for a more detailed study of the topic of 
  14. inheritance.
  15.  
  16.  
  17. REORGANIZED FILE STRUCTURE
  18. -----------------------------------------------------------------
  19. A close examination of the file named          ==================
  20. INHERIT1.CPP will reveal that it is identical     INHERIT1.CPP
  21. to the program developed in chapter 7 named    ==================
  22. ALLVEHIC.CPP except that the program text is 
  23. rearranged.  The biggest difference is that some of the simpler 
  24. methods in the classes have been changed to inline code to 
  25. shorten the file considerably.  In a practical programming 
  26. situation, methods that are this short should be programmed 
  27. inline since the actual code to return a simple value is shorter 
  28. than the code required to send a message to a non-inline method.
  29.  
  30. The only other change is the reordering of the classes and 
  31. associated methods with the classes all defined first, followed 
  32. by the main program.  This puts all class interface definitions 
  33. on a single page to make the code easier to study.  The 
  34. implementations for the methods are deferred until the end of 
  35. the file where they are available for quick reference but are not 
  36. cluttering up the class definitions which we wish to study 
  37. carefully in this chapter.  This should be an indication to you 
  38. that there is considerable flexibility in the way the classes and 
  39. methods can be arranged in C++.  Of course you realize that this 
  40. violates the spirit of C++ and its use of separate compilation, 
  41. but is only done here for convenience.  The best way to package 
  42. all of the example programs in this chapter is like the packaging 
  43. illustrated in chapter 7.
  44.  
  45. As mentioned before, the two derived classes, car and truck, each 
  46. have a variable named passenger_load which is perfectly legal, 
  47. and the car class has a method of the same name, initialize(), as 
  48. one defined in the super-class named vehicle.  The rearrangement 
  49. of the files in no way voids this allowable repeating of names.
  50.  
  51. After you have convinced yourself that this program is truly 
  52. identical to the program named ALLVEHIC.CPP from chapter 7, 
  53. compile and execute it with your compiler to assure yourself that 
  54. this arrangement is legal.  Due to this means of code packaging, 
  55. you will not need a "make" file or a "project" capability to 
  56.  
  57.                                                          Page 8-1
  58.  
  59.                                      Chapter 8 - More Inheritance
  60.  
  61. compile and execute this code.  This is to make it easy to 
  62. compile and execute the example programs in this chapter.
  63.  
  64.  
  65. THE SCOPE OPERATOR
  66. -----------------------------------------------------------------
  67. Because the method initialize() is defined in the derived car 
  68. class, it hides the method of the same name which is part of the 
  69. base class, and there may be times you wish to send a message to 
  70. the method in the base class for use in the derived class object.  
  71. This can be done by using the scope operator in the following 
  72. manner in the main program;
  73.  
  74.    sedan.vehicle::initialize(4, 3500.0);
  75.  
  76. As you might guess, the number and types of parameters must agree 
  77. with those of the method in the base class because it will 
  78. respond to the message.
  79.  
  80.  
  81. HIDDEN METHODS
  82. -----------------------------------------------------------------
  83. Examine the file named INHERIT2.CPP carefully  ==================
  84. and you will notice that it is a repeat of        INHERIT2.CPP
  85. the last example program with a few minor      ==================
  86. changes.
  87.  
  88. You will notice that the derived classes named car and truck do 
  89. not have the keyword public prior to the name of the base class 
  90. in the first line of each.  The keyword public, when included 
  91. prior to the base class name, makes all of the methods defined in 
  92. the base class available for use in the derived class just as if 
  93. they were defined as part of the derived class.  Therefore, in 
  94. the previous program, we were permitted to call the methods 
  95. defined as part of the base class from the main program even 
  96. though we were working with an object of one of the derived 
  97. classes.  In this program, the methods are inherited as private 
  98. and are therefore unavailable to any code outside of this class.  
  99. One example of when we did this, was when we sent a message to 
  100. the sedan to get its weight in an output statement of the main 
  101. program.
  102.  
  103. In the present program, without the keyword public prior to the 
  104. base class name, the only methods available for objects of the 
  105. car class, are those that are defined as part of the class 
  106. itself, and therefore we only have the methods named initialize() 
  107. and passengers() available for use with objects of class car.
  108.  
  109. When we declare an object of type car, according to the 
  110. definition of the C++ language, it contains three variables.  It 
  111. contains the one defined as part of its class named 
  112. passenger_load and the two that are part of its parent class, 
  113. wheels and weight.  All are available for direct use within its 
  114.  
  115.                                                          Page 8-2
  116.  
  117.                                      Chapter 8 - More Inheritance
  118.  
  119. methods because of the use of the keyword protected in the base 
  120. class.  The variables are a part of an object of class car when 
  121. it is declared and are stored as part of the object.  We will 
  122. show you the details of access to the parent class variables 
  123. within derived classes shortly in this chapter.  For now, we will 
  124. return to the use of the subclasses in this example program.
  125.  
  126. The observant student will notice that several of the output 
  127. statements have been commented out of the main program since they 
  128. are no longer legal or meaningful operations.  Lines 57 through 
  129. 59 have been commented out because the methods named get_weight() 
  130. and wheel_loading() are not available as members of the car class 
  131. without the keyword public in the car class definition.  You will 
  132. notice that initialize() is still available but this is the one 
  133. in the car class, not the method of the same name in the vehicle 
  134. class.
  135.  
  136. Moving on to the use of the truck class in the main program, we 
  137. find that lines 63 and 65 are commented out for the same reason 
  138. as given above, but lines 66 and 67 are commented out for an 
  139. entirely different reason.  Even though the method named 
  140. efficiency() is available and can be called as a part of the 
  141. truck class, it cannot be used because we have no way to 
  142. initialize the wheels or weight of the truck objects.  We can get 
  143. the weight of the truck objects, as we have done in line 108, but 
  144. since the weight has no way to be initialized, the result is 
  145. meaningless and lines 66 and 67 are commented out.
  146.  
  147. Be sure to compile and execute this example program to see that 
  148. your compiler gives the same result.  It would be a good exercise 
  149. for you to reintroduce some of the commented out lines to see 
  150. what sort of an error message your compiler issues for these 
  151. errors.
  152.  
  153.  
  154. INITIALIZING ALL DATA
  155. -----------------------------------------------------------------
  156. If you will examine the example program named  ==================
  157. INHERIT3.CPP, you will find that we have          INHERIT3.CPP
  158. fixed the initialization problem that we left  ==================
  159. dangling in the last example program.
  160.  
  161. The method named init_truck() now contains all four of the 
  162. parameters as input data which get transferred to the four 
  163. variables.  Following the initialization, it is permissible to 
  164. call the semi.efficiency() method in line 67 and 68 of the main 
  165. program.  Be sure to compile and execute this program following 
  166. your detailed study of it.
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.                                                          Page 8-3
  174.  
  175.                                      Chapter 8 - More Inheritance
  176.  
  177. WHAT IS PROTECTED DATA?
  178. -----------------------------------------------------------------
  179. Examine the program named INHERIT4.CPP for an  ==================
  180. example we will use to define protected data.     INHERIT4.CPP
  181. Just to make the program more versatile, we    ==================
  182. have returned to the use of the keyword public 
  183. prior to the name of the parent classes in lines 18 and 29 of the 
  184. class definitions which results in public inheritance.
  185.  
  186. If the data within a base class were totally available in all 
  187. classes inheriting that base class, it would be a simple matter 
  188. for a programmer to inherit the superclass into a derived class 
  189. and have free access to all data in the parent class.  This would 
  190. completely override the protection afforded by the use of 
  191. information hiding.  For this reason, the data in a class are 
  192. not automatically available to the methods of an inheriting 
  193. class.  There are times when you may wish to automatically 
  194. inherit all variables directly into the subclasses and have them 
  195. act just as though they were defined as a part of those classes 
  196. also.  For this reason, the designer of C++ has provided the 
  197. keyword protected.
  198.  
  199. In the present example program, the keyword protected is given in 
  200. line 5 so that all of the data of the vehicle class can be 
  201. directly imported into any derived classes but are not available 
  202. outside of the class or derived classes.  All data are 
  203. automatically defaulted to private type if no specifier is given.  
  204. The keyword private can be used as illustrated in lines 19 and 30 
  205. but adds nothing due to the fact that class members default to 
  206. private by definition at the beginning of a class.
  207.  
  208. You will notice that the variables named wheels and weight are 
  209. available to use in the method named initialize() in lines 86 
  210. through 92 just as if they were declared as a part of the car 
  211. class itself.  We can now state the rules for the three means of 
  212. defining variables and methods.
  213.  
  214.     private - The variables and methods are not available to 
  215.         any outside calling routines, and they are not available 
  216.         to any derived classes inheriting this class.
  217.  
  218.     protected - The variables and methods are not available 
  219.         to any outside calling routines, but they are directly 
  220.         available to any derived class inheriting this class.
  221.  
  222.     public - All variables and methods are freely available 
  223.         to all outside calling routines and to all derived 
  224.         classes.
  225.  
  226. You will note that these three means of definition can also be 
  227. used in a struct type.  The only difference with a struct is that 
  228. everything defaults to public until one of the other keywords is 
  229. used.
  230.  
  231.                                                          Page 8-4
  232.  
  233.                                      Chapter 8 - More Inheritance
  234.  
  235. Be sure to compile and execute this program before continuing on 
  236. to the next example program.
  237.  
  238.  
  239. WHAT IS PRIVATE DATA?
  240. -----------------------------------------------------------------
  241. Examine the file named INHERIT5.CPP where the  ==================
  242. data is allowed to use the private default.       INHERIT5.CPP
  243. In this program, the data is not available     ==================
  244. for use in the derived classes, so the only 
  245. way the data in the base class can be used is through use of 
  246. messages to methods in the base class.
  247.  
  248. It seems a little silly to have to call methods in the base class 
  249. to get to the data which is actually a part of the derived class, 
  250. but that is the way C++ is defined to work.  This would indicate 
  251. to you that you should spend some time thinking about how any 
  252. class you define will be used.  If you think somebody may wish to 
  253. inherit your class into a new class and expand it, you should 
  254. make the data members protected so they can be easily used in the 
  255. new class.  Be sure to compile and execute this program.
  256.  
  257.  
  258. INHERITING CONSTRUCTORS
  259. -----------------------------------------------------------------
  260. Examine the example program named INHERIT6.CPP   ================
  261. for yet another variation to our basic             INHERIT6.CPP
  262. program, this time adding constructors.  The     ================
  263. vehicle class has a constructor to initialize 
  264. the number of wheels and the weight to the indicated values and 
  265. has no surprising constructs.  The car and truck classes each 
  266. have a constructor also to initialize their unique variables to 
  267. some unique values.  If you jump ahead to the main program, you 
  268. will find that the initializing statements are commented out for 
  269. each of the objects so we must depend on the constructors to 
  270. initialize the variables.  The most important thing to glean from 
  271. this example program is the fact that when a constructor is 
  272. called for a derived class, a constructor is also called for the 
  273. parent class.  In fact, the constructor for the parent class will 
  274. be called before the constructor for the derived class is called.  
  275. All of the data will be initialized, including the data inherited 
  276. from the parent class.
  277.  
  278. We will say much more about constructors used with inheritance in 
  279. the next chapter of this tutorial.  Be sure to compile and 
  280. execute this example program.
  281.  
  282.  
  283. POINTERS TO AN OBJECT AND AN ARRAY OF OBJECTS
  284. -----------------------------------------------------------------
  285. Examine the example program named              ==================
  286. INHERIT7.CPP for examples of the use of an        INHERIT7.CPP
  287. array of objects and a pointer to an object.   ==================
  288.  
  289.                                                          Page 8-5
  290.  
  291.                                      Chapter 8 - More Inheritance
  292.  
  293. In this program, the objects are instantiated 
  294. from an inherited class and the intent of this program is to 
  295. illustrate that there is nothing magic about a derived class.
  296.  
  297. The program is identical to the first program in this chapter 
  298. until we get to the main program where we find an array of 3 
  299. objects of class car declared in line 52.  It should be obvious 
  300. that any operation that is legal for a simple object is legal for 
  301. an object that is part of an array, but we must be sure to tell 
  302. the system which object of the array we are interested in by 
  303. adding the array subscript as we do in lines 56 through 62.  The 
  304. operation of this portion of the program should be very easy for 
  305. you to follow, so we will go on to the next construct of 
  306. interest.
  307.  
  308. You will notice, in line 65, that we do not declare an object of 
  309. type truck but a pointer to an object of type truck.  In order to 
  310. use the pointer, we must give it something to point at which we 
  311. do in line 67 by dynamically allocating an object.  Once the 
  312. pointer has an object to point to, we can use the object in the 
  313. same way we would use any object, but we must use the pointer 
  314. notation to access any of the methods of the object.  This is 
  315. illustrated for you in lines 68 through 72, and will be further 
  316. illustrated in the example program of chapter 12 in this 
  317. tutorial.
  318.  
  319. Finally, we deallocate the object in line 73.  You should spend 
  320. enough time with this program to thoroughly understand the new 
  321. material presented here, then compile and execute it.
  322.  
  323.  
  324. THE NEW TIME CLASS
  325. -----------------------------------------------------------------
  326. We began a series of nontrivial classes in chapter 5 where we 
  327. developed a date class, then a time class, and finally a newdate 
  328. class in the last chapter.  Now it is your turn to add to this 
  329. series.  Your assignment is to develop the newtime class which 
  330. inherits the time class and adds a new member variable named 
  331. seconds_today and a method to calculate the value of seconds 
  332. since midnight to fill the variable.
  333.  
  334. A complete solution to this problem will be found in the ANSWERS 
  335. directory on the distribution disk.  The files named NEWTIME.H, 
  336. NEWTIME.CPP, and TRYNTIME.CPP are the solution files.  It would 
  337. be a good exercise for you to attempt to write this new class 
  338. before you look at the example solution.
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.                                                          Page 8-6
  348.  
  349.                                      Chapter 8 - More Inheritance
  350.  
  351. PROGRAMMING EXERCISE
  352. -----------------------------------------------------------------
  353. 1.  Remove the comment delimiters from lines 65 through 67 of 
  354.     INHERIT2.CPP to see what kind of results are returned.  
  355.     Remove them from line 57 to see what kind of an error is 
  356.     reported by the compiler for this error.2.   Add cout 
  357.     statements to each of the constructors of INHERIT5.CPP to 
  358.     output messages to the monitor so you can see the order of 
  359.     sending messages to the constructors.
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.                                                          Page 8-7
  406.