Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. #include <iostream>
  2. #include "Sales_item.h"
  3.  
  4. int main()
  5. {
  6. Sales_item nonzero = 0;
  7. while (std::cin >> nonzero)
  8. std::cout << nonzero;
  9. return 0;
  10. }
  11.  
  12. #ifndef SALESITEM_H
  13. // we're here only if SALESITEM_H has not yet been defined
  14. #define SALESITEM_H
  15.  
  16. #include "Version_test.h"
  17.  
  18. // Definition of Sales_item class and related functions goes here
  19. #include <iostream>
  20. #include <string>
  21.  
  22. class Sales_item {
  23. // these declarations are explained section 7.2.1, p. 270
  24. // and in chapter 14, pages 557, 558, 561
  25. friend std::istream& operator>>(std::istream&, Sales_item&);
  26. friend std::ostream& operator<<(std::ostream&, const Sales_item&);
  27. friend bool operator<(const Sales_item&, const Sales_item&);
  28. friend bool
  29. operator==(const Sales_item&, const Sales_item&);
  30. public:
  31. // constructors are explained in section 7.1.4, pages 262 - 265
  32. // default constructor needed to initialize members of built-in type
  33. #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
  34. Sales_item() = default;
  35. #else
  36. Sales_item(): units_sold(0), revenue(0.0) { }
  37. #endif
  38. Sales_item(const std::string &book):
  39. bookNo(book), units_sold(0), revenue(0.0) { }
  40. Sales_item(std::istream &is) { is >> *this; }
  41. public:
  42. // operations on Sales_item objects
  43. // member binary operator: left-hand operand bound to implicit this pointer
  44. Sales_item& operator+=(const Sales_item&);
  45.  
  46. // operations on Sales_item objects
  47. std::string isbn() const { return bookNo; }
  48. double avg_price() const;
  49. // private members as before
  50. private:
  51. std::string bookNo; // implicitly initialized to the empty string
  52. #ifdef IN_CLASS_INITS
  53. unsigned units_sold = 0; // explicitly initialized
  54. double revenue = 0.0;
  55. #else
  56. unsigned units_sold;
  57. double revenue;
  58. #endif
  59. };
  60.  
  61. // used in chapter 10
  62. inline
  63. bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
  64. { return lhs.isbn() == rhs.isbn(); }
  65.  
  66. // nonmember binary operator: must declare a parameter for each operand
  67. Sales_item operator+(const Sales_item&, const Sales_item&);
  68.  
  69. inline bool
  70. operator==(const Sales_item &lhs, const Sales_item &rhs)
  71. {
  72. // must be made a friend of Sales_item
  73. return lhs.units_sold == rhs.units_sold &&
  74. lhs.revenue == rhs.revenue &&
  75. lhs.isbn() == rhs.isbn();
  76. }
  77.  
  78. inline bool
  79. operator!=(const Sales_item &lhs, const Sales_item &rhs)
  80. {
  81. return !(lhs == rhs); // != defined in terms of operator==
  82. }
  83.  
  84. // assumes that both objects refer to the same ISBN
  85. Sales_item& Sales_item::operator+=(const Sales_item& rhs)
  86. {
  87. units_sold += rhs.units_sold;
  88. revenue += rhs.revenue;
  89. return *this;
  90. }
  91.  
  92. // assumes that both objects refer to the same ISBN
  93. Sales_item
  94. operator+(const Sales_item& lhs, const Sales_item& rhs)
  95. {
  96. Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
  97. ret += rhs; // add in the contents of (|rhs|)
  98. return ret; // return (|ret|) by value
  99. }
  100.  
  101. std::istream&
  102. operator>>(std::istream& in, Sales_item& s)
  103. {
  104. double price;
  105. in >> s.bookNo >> s.units_sold >> price;
  106. // check that the inputs succeeded
  107. if (in)
  108. s.revenue = s.units_sold * price;
  109. else
  110. s = Sales_item(); // input failed: reset object to default state
  111. return in;
  112. }
  113.  
  114. std::ostream&
  115. operator<<(std::ostream& out, const Sales_item& s)
  116. {
  117. out << s.isbn() << " " << s.units_sold << " "
  118. << s.revenue << " " << s.avg_price();
  119. return out;
  120. }
  121.  
  122. inline bool operator<(const Sales_item &, const Sales_item &)
  123. {
  124. return false;
  125. }
  126.  
  127. double Sales_item::avg_price() const
  128. {
  129. if (units_sold)
  130. return revenue/units_sold;
  131. else
  132. return 0;
  133. }
  134. #endif
  135.  
  136. #ifndef VERSION_TEST_H
  137. #define VERSION_TEST_H
  138.  
  139. /* As of the first printing of C++ Primer, 5th Edition (July 2012),
  140. * the Microsoft Complier did not yet support a number of C++ 11 features.
  141. *
  142. * The code we distribute contains both normal C++ code and
  143. * workarounds for missing features. We use a series of CPP variables to
  144. * determine whether a given features is implemented in a given release
  145. * of the MS compiler. The base version we used to test the code in the book
  146. * is Compiler Version 17.00.50522.1 for x86.
  147. *
  148. * When new releases are available we will update this file which will
  149. * #define the features implmented in that release.
  150. */
  151.  
  152. #if _MSC_FULL_VER == 170050522 || _MSC_FULL_VER == 170050727
  153. // base version, future releases will #define those features as they are
  154. // implemented by Microsoft
  155.  
  156. /* Code in this delivery use the following variables to control compilation
  157.  
  158. Variable tests C++ 11 Feature
  159. CONSTEXPR_VARS constexpr variables
  160. CONSTEXPR_FCNS constexpr functions
  161. CONSTEXPR_CTORS constexpr constructors and other member functions
  162. DEFAULT_FCNS = default
  163. DELETED_FCNS = delete
  164. FUNC_CPP __func__ local static
  165. FUNCTION_PTRMEM function template with pointer to member function
  166. IN_CLASS_INITS in class initializers
  167. INITIALIZER_LIST library initializer_list<T> template
  168. LIST_INIT list initialization of ordinary variables
  169. LROUND lround function in cmath
  170. NOEXCEPT noexcept specifier and noexcept operator
  171. SIZEOF_MEMBER sizeof class_name::member_name
  172. TEMPLATE_FCN_DEFAULT_ARGS default template arguments for function templates
  173. TYPE_ALIAS_DECLS type alias declarations
  174. UNION_CLASS_MEMS unions members that have constructors or copy control
  175. VARIADICS variadic templates
  176. */
  177. #endif // ends compiler version check
  178.  
  179. #ifndef LROUND
  180. inline long lround(double d)
  181. {
  182. return (d >= 0) ? long(d + 0.5) : long(d - 0.5);
  183. }
  184. #endif
  185.  
  186. #endif // ends header guard
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement