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