fork download
  1. /*
  2.  * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
  3.  * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  *
  6.  * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
  7.  *
  8.  *
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  *
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address:
  22.  *
  23.  * Pearson Education, Inc.
  24.  * Rights and Permissions Department
  25.  * One Lake Street
  26.  * Upper Saddle River, NJ 07458
  27.  * Fax: (201) 236-3290
  28. */
  29.  
  30. /* This file defines the Sales_item class used in chapter 1.
  31.  * The code used in this file will be explained in
  32.  * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
  33.  * Readers shouldn't try to understand the code in this file
  34.  * until they have read those chapters.
  35. */
  36.  
  37. #ifndef SALESITEM_H
  38. // we're here only if SALESITEM_H has not yet been defined
  39. #define SALESITEM_H
  40.  
  41. // Definition of Sales_item class and related functions goes here
  42. #include <iostream>
  43. #include <string>
  44.  
  45. class Sales_item {
  46. // these declarations are explained section 7.2.1, p. 270
  47. // and in chapter 14, pages 557, 558, 561
  48. friend std::istream& operator>>(std::istream&, Sales_item&);
  49. friend std::ostream& operator<<(std::ostream&, const Sales_item&);
  50. friend bool operator<(const Sales_item&, const Sales_item&);
  51. friend bool
  52. operator==(const Sales_item&, const Sales_item&);
  53. public:
  54. // constructors are explained in section 7.1.4, pages 262 - 265
  55. // default constructor needed to initialize members of built-in type
  56. Sales_item() = default;
  57. Sales_item(const std::string &book): bookNo(book) { }
  58. Sales_item(std::istream &is) { is >> *this; }
  59. public:
  60. // operations on Sales_item objects
  61. // member binary operator: left-hand operand bound to implicit this pointer
  62. Sales_item& operator+=(const Sales_item&);
  63.  
  64. // operations on Sales_item objects
  65. std::string isbn() const { return bookNo; }
  66. double avg_price() const;
  67. // private members as before
  68. private:
  69. std::string bookNo; // implicitly initialized to the empty string
  70. unsigned units_sold = 0; // explicitly initialized
  71. double revenue = 0.0;
  72. };
  73.  
  74. // used in chapter 10
  75. inline
  76. bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
  77. { return lhs.isbn() == rhs.isbn(); }
  78.  
  79. // nonmember binary operator: must declare a parameter for each operand
  80. Sales_item operator+(const Sales_item&, const Sales_item&);
  81.  
  82. inline bool
  83. operator==(const Sales_item &lhs, const Sales_item &rhs)
  84. {
  85. // must be made a friend of Sales_item
  86. return lhs.units_sold == rhs.units_sold &&
  87. lhs.revenue == rhs.revenue &&
  88. lhs.isbn() == rhs.isbn();
  89. }
  90.  
  91. inline bool
  92. operator!=(const Sales_item &lhs, const Sales_item &rhs)
  93. {
  94. return !(lhs == rhs); // != defined in terms of operator==
  95. }
  96.  
  97. // assumes that both objects refer to the same ISBN
  98. Sales_item& Sales_item::operator+=(const Sales_item& rhs)
  99. {
  100. units_sold += rhs.units_sold;
  101. revenue += rhs.revenue;
  102. return *this;
  103. }
  104.  
  105. // assumes that both objects refer to the same ISBN
  106. Sales_item
  107. operator+(const Sales_item& lhs, const Sales_item& rhs)
  108. {
  109. Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
  110. ret += rhs; // add in the contents of (|rhs|)
  111. return ret; // return (|ret|) by value
  112. }
  113.  
  114. std::istream&
  115. operator>>(std::istream& in, Sales_item& s)
  116. {
  117. double price;
  118. in >> s.bookNo >> s.units_sold >> price;
  119. // check that the inputs succeeded
  120. if (in)
  121. s.revenue = s.units_sold * price;
  122. else
  123. s = Sales_item(); // input failed: reset object to default state
  124. return in;
  125. }
  126.  
  127. std::ostream&
  128. operator<<(std::ostream& out, const Sales_item& s)
  129. {
  130. out << s.isbn() << " " << s.units_sold << " "
  131. << s.revenue << " " << s.avg_price();
  132. return out;
  133. }
  134.  
  135. double Sales_item::avg_price() const
  136. {
  137. if (units_sold)
  138. return revenue/units_sold;
  139. else
  140. return 0;
  141. }
  142. #endif
  143.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty