Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1.  
  2. /* This file defines the Sales_item class used in chapter 1.
  3. * The code used in this file will be explained in
  4. * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
  5. * Readers shouldn't try to understand the code in this file
  6. * until they have read those chapters.
  7. */
  8.  
  9. #ifndef SALESITEM_H
  10. // we're here only if SALESITEM_H has not yet been defined
  11. #define SALESITEM_H
  12.  
  13. #include "Version_test.h"
  14.  
  15. // Definition of Sales_item class and related functions goes here
  16. #include <iostream>
  17. #include <string>
  18.  
  19. class Sales_item {
  20. // these declarations are explained section 7.2.1, p. 270
  21. // and in chapter 14, pages 557, 558, 561
  22. friend std::istream& operator>>(std::istream&, Sales_item&);
  23. friend std::ostream& operator<<(std::ostream&, const Sales_item&);
  24. friend bool operator<(const Sales_item&, const Sales_item&);
  25. friend bool
  26. operator==(const Sales_item&, const Sales_item&);
  27. public:
  28.  
  29.  
  30. #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
  31. Sales_item() = default;
  32. #else
  33. Sales_item(): units_sold(0), revenue(0.0) { }
  34. #endif
  35. Sales_item(const std::string &book):
  36. bookNo(book), units_sold(0), revenue(0.0) { }
  37. Sales_item(std::istream &is) { is >> *this; }
  38. public:
  39. // operations on Sales_item objects
  40. // member binary operator: left-hand operand bound to implicit this pointer
  41. Sales_item& operator+=(const Sales_item&);
  42.  
  43. // operations on Sales_item objects
  44. std::string isbn() const { return bookNo; }
  45. double avg_price() const;
  46. // private members as before
  47. private:
  48. std::string bookNo; // implicitly initialized to the empty string
  49. #ifdef IN_CLASS_INITS
  50. unsigned units_sold = 0;
  51. double revenue = 0.0;
  52. #else
  53. unsigned units_sold;
  54. double revenue;
  55. #endif
  56. };
  57.  
  58. // used in chapter 10
  59. inline
  60. bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
  61. { return lhs.isbn() == rhs.isbn(); }
  62.  
  63. // nonmember binary operator: must declare a parameter for each operand
  64. Sales_item operator+(const Sales_item&, const Sales_item&);
  65.  
  66. inline bool
  67. operator==(const Sales_item &lhs, const Sales_item &rhs)
  68. {
  69. // must be made a friend of Sales_item
  70. return lhs.units_sold == rhs.units_sold &&
  71. lhs.revenue == rhs.revenue &&
  72. lhs.isbn() == rhs.isbn();
  73. }
  74.  
  75. inline bool
  76. operator!=(const Sales_item &lhs, const Sales_item &rhs)
  77. {
  78. return !(lhs == rhs); // != defined in terms of operator==
  79. }
  80.  
  81. // assumes that both objects refer to the same ISBN
  82. Sales_item& Sales_item::operator+=(const Sales_item& rhs)
  83. {
  84. units_sold += rhs.units_sold;
  85. revenue += rhs.revenue;
  86. return *this;
  87. }
  88.  
  89. // assumes that both objects refer to the same ISBN
  90. Sales_item
  91. operator+(const Sales_item& lhs, const Sales_item& rhs)
  92. {
  93. Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
  94. ret += rhs; // add in the contents of (|rhs|)
  95. return ret; // return (|ret|) by value
  96. }
  97.  
  98. std::istream&
  99. operator>>(std::istream& in, Sales_item& s)
  100. {
  101. double price;
  102. in >> s.bookNo >> s.units_sold >> price;
  103. // check that the inputs succeeded
  104. if (in)
  105. s.revenue = s.units_sold * price;
  106. else
  107. s = Sales_item(); // input failed: reset object to default state
  108. return in;
  109. }
  110.  
  111. std::ostream&
  112. operator<<(std::ostream& out, const Sales_item& s)
  113. {
  114. out << s.isbn() << " " << s.units_sold << " "
  115. << s.revenue << " " << s.avg_price();
  116. return out;
  117. }
  118.  
  119. double Sales_item::avg_price() const
  120. {
  121. if (units_sold)
  122. return revenue/units_sold;
  123. else
  124. return 0;
  125. }
  126. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement