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

  1.  
  2.  
  3.  
  4.                                                         Chapter 2
  5.                                                    COMPOUND TYPES
  6.  
  7. ENUMERATED TYPES
  8. -----------------------------------------------------------------
  9. Examine the file named ENUM.CPP for an         ==================
  10. example that uses an enumerated type                ENUM.CPP
  11. variable.  The enumerated type is used in C++  ==================
  12. in a very similar way that it was used in 
  13. ANSI-C.  The keyword enum is not required to be used again when 
  14. defining a variable of that type, but it can be used if desired.  
  15. The name game_result is defined as an enumerated type making the 
  16. use of the keyword enum optional.  However, it may be clearer for 
  17. you to use the keyword when defining a variable in the same 
  18. manner that it is required to be used in C, and you may choose to 
  19. do so.
  20.  
  21. The example program uses the keyword enum in line 9, but omits it 
  22. in line 8 to illustrate to you that it is indeed optional, but 
  23. that is a trivial difference.  There is a bigger difference in 
  24. the way an enumerated type is used in C++.  In C, the enumerated 
  25. type is simply an int type variable, but in C++ it is not an int, 
  26. but its own type.  Mathematical operations can not be performed 
  27. on it, nor can an integer be assigned to it.  It cannot be 
  28. incremented or decremented as it can be in C.  In the example 
  29. program, an integer is used for the for loop because it can be 
  30. incremented, then the value of the loop index is assigned to the 
  31. enumerated variable by using a cast.  The cast is required or a 
  32. compile error is reported.  The mathematical operations and the 
  33. increment and decrement operators can be defined for the 
  34. enumerated type, but they are not automatically available.  
  35. Operator overloading will be studied later, and the last sentence 
  36. will make much more sense at that time.
  37.  
  38. The remainder of this program should be no problem for you to 
  39. understand.  After studying it, be sure to compile and execute it 
  40. and examine the output.
  41.  
  42.  
  43. A SIMPLE STRUCTURE
  44. -----------------------------------------------------------------
  45. Examine the example program named              ==================
  46. STRUCTUR.CPP for an illustration using a very     STRUCTUR.CPP
  47. simple structure.  This structure is no        ==================
  48. different from that used in ANSI-C except for 
  49. the fact that the keyword struct is not required to be used again 
  50. when defining a variable of that type.  Lines 11 and 12 
  51. illustrate the declaration of variables without the keyword, and 
  52. line 13 indicates that the keyword struct can be included if 
  53. desired.  It is up to you to choose which style you prefer to use 
  54. in your C++ programs.
  55.  
  56.  
  57.                                                          Page 2-1
  58.  
  59.                                        Chapter 2 - Compound Types
  60.  
  61. You should take note of the fact that this is a valid ANSI-C 
  62. program except for the fact that it uses the stream library, the 
  63. C++ comments, and the lack of use of the keyword struct in two of 
  64. the lines.
  65.  
  66. Once again, be sure to compile and execute this program after 
  67. studying it carefully, because the next example program is very 
  68. similar but it introduces a brand new construct not available in 
  69. standard C, the class.
  70.  
  71.  
  72. A VERY SIMPLE CLASS
  73. -----------------------------------------------------------------
  74. Examine the example program named CLASS1.CPP   ==================
  75. for our first example of a class in C++.           CLASS1.CPP
  76. This is the first class example, but it will   ==================
  77. not be the last, since the class is the major 
  78. reason for using C++ over ANSI-C or some other programming 
  79. language.  You will notice the keyword class used in line 4, in 
  80. exactly the same way that the keyword struct was used in the last 
  81. program, and they are in fact very similar constructs.  There are 
  82. definite differences, as we will see, but for the present time we 
  83. will be concerned more with their similarities.
  84.  
  85. The word animal in line 4 is the name of the class, and when we 
  86. declare variables of this type in lines 12 through 14, we can 
  87. either omit the keyword class or include it if desired as 
  88. illustrated in line 14.  In the last program, we declared 5 
  89. variables of a structure type, but in this program we declare 5 
  90. objects.  They are called objects because they are of a class 
  91. type.  The differences are subtle, and in this case the 
  92. differences are negligible, but as we proceed through this 
  93. tutorial, we will see that the class construct is indeed very 
  94. important and valuable.  The class was introduced here only to 
  95. give you a glimpse of what is to come later in this tutorial.
  96.  
  97. The class is a type which can be used to declare objects in much 
  98. the same way that a structure is a type that can be used to 
  99. declare variables.  Your dog named King is a specific instance 
  100. of the general class of dogs, and in a similar manner, an object 
  101. is a specific instance of a class.  It would be well to take note 
  102. of the fact that the class is such a generalized concept that 
  103. there are libraries of prewritten classes available in the 
  104. marketplace.  You can purchase classes which perform some 
  105. generalized operations such as managing stacks, queues, or lists, 
  106. sorting data, managing windows, etc.  This is because of the 
  107. generality and flexibility of the class construct.
  108.  
  109. The new keyword public in line 5, followed by a colon, is 
  110. necessary in this case because the variables in a class are 
  111. defaulted to a private type and we could not access them at all 
  112. without making them public.  Don't worry about this program yet, 
  113. we will cover all of this in great detail later in this tutorial.
  114.  
  115.                                                          Page 2-2
  116.  
  117.                                        Chapter 2 - Compound Types
  118.  
  119. Be sure to compile and run this example program to see that it 
  120. does what we say it does with your compiler.  Keep in mind that 
  121. this is your first example of a class and it illustrates 
  122. essentially nothing concerning the use of this powerful C++ 
  123. construct.
  124.  
  125.  
  126. THE FREE UNION OF C++
  127. -----------------------------------------------------------------
  128. Examine the program named UNIONEX.CPP for an    =================
  129. example of a free union.  In ANSI-C, all           UNIONEX.CPP
  130. unions must be named in order to be used, but   =================
  131. this is not true in C++.  When using C++ we 
  132. can use a free union, a union without a name.  The union is 
  133. embedded within a simple structure and you will notice that there 
  134. is not a variable name following the declaration of the union in 
  135. line 11.  In ANSI-C, we would have to name the union and give a 
  136. triple name (three names dotted together) to access the members.  
  137. Since it is a free union, there is no union name, and the 
  138. variables are accessed with only a doubly dotted name as 
  139. illustrated in lines 18, 22, 26, and others.
  140.  
  141. You will recall that a union causes all the data contained within 
  142. the union to be stored in the same physical memory locations, 
  143. such that only one variable is actually available at a time.  
  144. This is exactly what is happening here.  The variable named 
  145. fuel_load, bomb_load, and pallets are stored in the same physical 
  146. memory locations and it is up to the programmer to keep track of 
  147. which variable is stored there at any given time.  You will 
  148. notice that the transport is assigned a value for pallets in line 
  149. 26, then a value for fuel_load in line 28.  When the value for 
  150. fuel_load is assigned, the value for pallets is corrupted and is 
  151. no longer available since it was stored where fuel_load is 
  152. currently stored.  The observant student will notice that this is 
  153. exactly the way the union is used in ANSI-C except for the way 
  154. components are named.
  155.  
  156. The remainder of the program should be easy for you to 
  157. understand, so after you study and understand it, compile and 
  158. execute it.
  159.  
  160.  
  161. C++ TYPE CONVERSIONS
  162. -----------------------------------------------------------------
  163. Examine the program named TYPECONV.CPP for a   ==================
  164. few examples of type conversions in C++.  The     TYPECONV.CPP
  165. type conversions are done in C++ in exactly    ==================
  166. the same manner as they are done in ANSI-C, 
  167. but C++ gives you another form for doing the conversions.Lines 10 
  168. through 17 of this program use the familiar "cast" form of type 
  169. conversions used in ANSI-C, and there is nothing new to the 
  170. experienced C programmer.  You will notice that lines 10 through 
  171. 13 are actually all identical to each other.  The only difference 
  172.  
  173.                                                          Page 2-3
  174.  
  175.                                        Chapter 2 - Compound Types
  176.  
  177. is that we are coercing the compiler to do the indicated type 
  178. conversions prior to doing the addition and the assignment in 
  179. some of the statements.  In line 13, the int type variable will 
  180. be converted to type float prior to the addition, then the 
  181. resulting float will be converted to type char prior to being 
  182. assigned to the variable c.
  183.  
  184. Additional examples of type coercion are given in lines 15 
  185. through 17 and all three of these lines are essentially the same.
  186.  
  187. The examples given in lines 19 through 26 are unique to C++ and 
  188. are not valid in ANSI-C.  In these lines the type coercions are 
  189. written as though they are function calls instead of the more 
  190. familiar "cast" method as illustrated earlier.  Lines 19 through 
  191. 26 are identical to lines 10 through 17.
  192.  
  193. You may find this method of type coercion to be clearer and 
  194. easier to understand than the "cast" method and in C++ you are 
  195. free to use either, or to mix them if you so desire, but your 
  196. code could be very difficult to read if you indescriminantly mix 
  197. them.
  198.  
  199. Be sure to compile and execute this example program.
  200.  
  201.  
  202. PROGRAMMING EXERCISES
  203. -----------------------------------------------------------------
  204. 1.  Starting with the program ENUM.CPP, add the enumerated value 
  205.     of FORFEIT to the enumerated type game_result, and add a 
  206.     suitable message and logic to get the message printed in some 
  207.     way.
  208.  
  209. 2.  Add the variable height of type float to the class of 
  210.     CLASS1.CPP and store some values in the new variable.  Print 
  211.     some of the values out.  Move the new variable ahead of the 
  212.     keyword public: and see what kind of error message results.  
  213.     We will cover this error in chapter 5 of this tutorial.
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.                                                          Page 2-4
  232.