1 // add_item.cc 2 #include <iostream> 3 #include "Sales_item.h" 4 5 int main(int argc, char** argv) 6 { 7 Sales_item item1; 8 Sales_item item2; 9 10 std::cin >> item1 >> item2; // read a pair of transactions 11 std::cout << item1 + item2 << std::endl; // print their sum 12 13 return 0; 14 }
1 // Sales_item.h 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 // Definition of Sales_item class and related functions goes here 14 #include <iostream> 15 #include <string> 16 17 class Sales_item { 18 // these declarations are explained section 7.2.1, p. 270 19 // and in chapter 14, pages 557, 558, 561 20 friend std::istream& operator>>(std::istream&, Sales_item&); 21 friend std::ostream& operator<<(std::ostream&, const Sales_item&); 22 friend bool operator<(const Sales_item&, const Sales_item&); 23 friend bool operator==(const Sales_item&, const Sales_item&); 24 public: 25 // constructors are explained in section 7.1.4, pages 262 - 265 26 // default constructor needed to initialize members of built-in type 27 Sales_item() = default; 28 Sales_item(const std::string &book): bookNo(book) { } 29 Sales_item(std::istream &is) { is >> *this; } 30 public: 31 // operations on Sales_item objects 32 // member binary operator: left-hand operand bound to implicit this pointer 33 Sales_item& operator+=(const Sales_item&); 34 35 // operations on Sales_item objects 36 std::string isbn() const { return bookNo; } 37 double avg_price() const; 38 // private members as before 39 private: 40 std::string bookNo; // implicitly initialized to the empty string 41 unsigned units_sold = 0; // explicitly initialized 42 double revenue = 0.0; 43 }; 44 45 // used in chapter 10 46 inline 47 bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 48 { return lhs.isbn() == rhs.isbn(); } 49 // nonmember binary operator: must declare a parameter for each operand 50 Sales_item operator+(const Sales_item&, const Sales_item&); 51 52 inline bool operator==(const Sales_item &lhs, const Sales_item &rhs) 53 { 54 // must be made a friend of Sales_item 55 return lhs.units_sold == rhs.units_sold && 56 lhs.revenue == rhs.revenue && 57 lhs.isbn() == rhs.isbn(); 58 } 59 60 inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) 61 { 62 return !(lhs == rhs); // != defined in terms of operator== 63 } 64 65 // assumes that both objects refer to the same ISBN 66 Sales_item& Sales_item::operator+=(const Sales_item& rhs) 67 { 68 units_sold += rhs.units_sold; 69 revenue += rhs.revenue; 70 return *this; 71 } 72 73 // assumes that both objects refer to the same ISBN 74 Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) 75 { 76 Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return 77 ret += rhs; // add in the contents of (|rhs|) 78 return ret; // return (|ret|) by value 79 } 80 81 std::istream& operator>>(std::istream& in, Sales_item& s) 82 { 83 double price; 84 in >> s.bookNo >> s.units_sold >> price; 85 // check that the inputs succeeded 86 if (in) 87 s.revenue = s.units_sold * price; 88 else 89 s = Sales_item(); // input failed: reset object to default state 90 return in; 91 } 92 93 std::ostream& operator<<(std::ostream& out, const Sales_item& s) 94 { 95 out << s.isbn() << " " << s.units_sold << " " 96 << s.revenue << " " << s.avg_price(); 97 return out; 98 } 99 100 double Sales_item::avg_price() const 101 { 102 if (units_sold) 103 return revenue/units_sold; 104 else 105 return 0; 106 } 107 108 #endif
1 AddItem: add_item.cc 2 >---g++ -g -Wall -std=c++0x add_item.cc -o AddItem 3 clean: 4 >---rm AddItem