fork(1) download
  1. /*
  2. 1.5.1. The Sales_item Class
  3.  
  4. The purpose of the Sales_item class is to represent the total revenue, number of copies sold,
  5. and average sales price for a book. How these data are stored or computed is not our concern.
  6. To use a class, we need not care about how it is implemented. Instead, what we need to know is
  7. what operations objects of that type can perform.
  8.  
  9. Every class defines a type. The type name is
  10. the same as the name of the class. Hence, our Sales_item class defines a type named
  11. Sales_item. As with the built-in types, we can define a variable of a class type. When we
  12. write
  13.  
  14. Sales_item item;
  15.  
  16. we are saying that item is an object of type Sales_item. We often
  17. contract the phrase “an object of type Sales_item” to “a Sales_item object” or even more
  18. simply to “a Sales_item.” In addition to being able to define variables of type Sales_item, we can:
  19.  
  20. • Call a function named isbn to fetch the ISBN from a Sales_item object.
  21. • Use the input (>>) and output (<<) operators to read and write objects of type Sales_item.
  22. • Use the assignment operator (=) to assign one Sales_item object to another.
  23. • Use the addition operator (+) to add two Sales_item objects. The two objects must refer to the same ISBN. The result is a new Sales_item object whose ISBN is that of its operands and whose number sold and revenue are the sum of the corresponding values in its operands.
  24. • Use the compound assignment operator (+=) to add one Sales_item object into another.
  25.  
  26. Reading and Writing Sales_items
  27. Now that we know what operations we can use with Sales_item objects, we can write programs
  28. that use the class. For example, the following program reads data from the standard input into
  29. a Sales_item object and writes that Sales_item back onto the standard output:
  30. */
  31.  
  32. #include <iostream>
  33. #include "Sales_item.h"
  34. int main()
  35. {
  36. Sales_item book; // read ISBN, number of copies sold, and sales price
  37. std::cin >> book; // write ISBN, number of copies sold, total revenue, and average price
  38. std::cout << book << std::endl;
  39. return 0;
  40. }
  41.  
  42. /*
  43. If the input to this program is
  44.  
  45. 0-201-70353-X 4 24.99
  46.  
  47. then the output will be
  48.  
  49. 0-201-70353-X 4 99.96 24.99
  50.  
  51. Our input says that we sold four copies of the book at $24.99 each, and the output indicates
  52. that the total sold was four, the total revenue was $99.96, and the average price per book was
  53. $24.99.
  54.  
  55. This program starts with two #include directives, one of which uses a new form. Headers from
  56. the standard library are enclosed in angle brackets (< >). Those that are not part of the
  57. library are enclosed in double quotes (" ").
  58.  
  59. Inside main we define an object, named book, that we’ll use to hold the data that we read from
  60. the standard input. The next statement reads into that object, and the third statement prints
  61. it to the standard output followed by printing endl.
  62.  
  63. Adding Sales_items
  64.  
  65. A more interesting example adds two Sales_item objects:
  66. */
  67.  
  68. #include <iostream>
  69. #include "Sales_item.h"
  70. int main()
  71. {
  72. Sales_item item1, item2;
  73. std::cin >> item1 >> item2; // read a pair of transactions
  74. std::cout << item1 + item2 << std::endl; // print their sum
  75. return 0;
  76. }
  77.  
  78. /*
  79. If we give this program the following input
  80.  
  81. 0-201-78345-X 3 20.00
  82. 0-201-78345-X 2 25.00
  83.  
  84. our output is
  85.  
  86. 0-201-78345-X 5 110 22
  87.  
  88. This program starts by including the Sales_item and iostream headers. Next we define two
  89. Sales_item objects to hold the transactions. We read data into these objects from the standard
  90. input. The output expression does the addition and prints the result.
  91.  
  92. It’s worth noting how similar this program looks to the one on page 6: We read two inputs and
  93. write their sum. What makes this similarity noteworthy is that instead of reading and printing
  94. the sum of two integers, we’re reading and printing the sum of two Sales_item objects.
  95. Moreover, the whole idea of “sum” is different. In the case of ints we are generating a
  96. conventional sum—the result of adding two numeric values. In the case of Sales_item objects we
  97. use a conceptually new meaning for sum—the result of adding the components of two Sales_item
  98. objects.
  99. */
  100.  
  101. /*
  102. 1.5.2. A First Look at Member Functions
  103. Our program that adds two Sales_items should check whether the objects have the same ISBN.
  104. We’ll do so as follows:
  105. */
  106.  
  107. #include <iostream>
  108. #include "Sales_item.h"
  109. int main()
  110. {
  111. Sales_item item1, item2;
  112. std::cin >> item1 >> item2; // first check that item1 and item2 represent the same book
  113. if (item1.isbn() == item2.isbn())
  114. {
  115. std::cout << item1 + item2 << std::endl;
  116. return 0; // indicate success
  117. }
  118. else
  119. {
  120. std::cerr << "Data must refer to same ISBN"
  121. << std::endl;
  122. return -1; // indicate failure
  123. }
  124. }
  125.  
  126. /*
  127. The difference between this program and the previous version is the if and its associated else
  128. branch. Even without understanding the if condition, we know what this program does. If the
  129. condition succeeds, then we write the same output as before and return 0, indicating success.
  130. If the condition fails, we execute the block following the else, which prints a message and
  131. returns an error indicator.
  132.  
  133. What Is a Member Function?
  134.  
  135. The if condition
  136.  
  137. item1.isbn() == item2.isbn()
  138.  
  139. calls a member function named isbn. A member function is a function that is defined as part of
  140. a class. Member functions are sometimes referred to as methods.
  141.  
  142. Ordinarily, we call a member function on behalf of an object. For example, the first part of
  143. the left-hand operand of the equality expression
  144.  
  145. item1.isbn
  146.  
  147. uses the dot operator (the “.” operator) to say that we want “the isbn member of the object
  148. named item1.” The dot operator applies only to objects of class type. The left-hand operand
  149. must be an object of class type, and the right-hand operand must name a member of that type.
  150. The result of the dot operator is the member named by the right-hand operand.
  151.  
  152. When we use the dot operator to access a member function, we usually do so to call that
  153. function. We call a function using the call operator (the () operator). The call operator is a
  154. pair of parentheses that enclose a (possibly empty) list of arguments. The isbn member
  155. function does not take an argument. Thus,
  156.  
  157. item1.isbn()
  158.  
  159. calls the isbn function that is a member of the object named item1. This function returns the
  160. ISBN stored in item1. The right-hand operand of the equality operator executes in the same
  161. way—it returns the ISBN stored in item2. If the ISBNs are the same, the condition is true;
  162. otherwise it is false.
  163. */
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty