通过定义类可以定义自己的数据结构
重点了解三个问题:
1、类名是什么?(见下图)
2、它在哪里定义? (定义在Sales_item.h中)
3、它支持什么操作?(见下一节)
类的其他细节后面章节会将。
类定义代码贴在这里:
1 #ifndef SALESITEM_H 2 // we're here only if SALESITEM_H has not yet been defined 3 #define SALESITEM_H 4 5 #include "Version_test.h" 6 7 // Definition of Sales_item class and related functions goes here 8 #include <iostream> 9 #include <string> 10 11 class Sales_item { 12 // these declarations are explained section 7.2.1, p. 270 13 // and in chapter 14, pages 557, 558, 561 14 friend std::istream& operator>>(std::istream&, Sales_item&); 15 friend std::ostream& operator<<(std::ostream&, const Sales_item&); 16 friend bool operator<(const Sales_item&, const Sales_item&); 17 friend bool 18 operator==(const Sales_item&, const Sales_item&); 19 public: 20 // constructors are explained in section 7.1.4, pages 262 - 265 21 // default constructor needed to initialize members of built-in type 22 #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS) 23 Sales_item() = default; 24 #else 25 Sales_item(): units_sold(0), revenue(0.0) { } 26 #endif 27 Sales_item(const std::string &book): 28 bookNo(book), units_sold(0), revenue(0.0) { } 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 #ifdef IN_CLASS_INITS 42 unsigned units_sold = 0; // explicitly initialized 43 double revenue = 0.0; 44 #else 45 unsigned units_sold; 46 double revenue; 47 #endif 48 }; 49 50 // used in chapter 10 51 inline 52 bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 53 { return lhs.isbn() == rhs.isbn(); } 54 55 // nonmember binary operator: must declare a parameter for each operand 56 Sales_item operator+(const Sales_item&, const Sales_item&); 57 58 inline bool 59 operator==(const Sales_item &lhs, const Sales_item &rhs) 60 { 61 // must be made a friend of Sales_item 62 return lhs.units_sold == rhs.units_sold && 63 lhs.revenue == rhs.revenue && 64 lhs.isbn() == rhs.isbn(); 65 } 66 67 inline bool 68 operator!=(const Sales_item &lhs, const Sales_item &rhs) 69 { 70 return !(lhs == rhs); // != defined in terms of operator== 71 } 72 73 // assumes that both objects refer to the same ISBN 74 Sales_item& Sales_item::operator+=(const Sales_item& rhs) 75 { 76 units_sold += rhs.units_sold; 77 revenue += rhs.revenue; 78 return *this; 79 } 80 81 // assumes that both objects refer to the same ISBN 82 Sales_item 83 operator+(const Sales_item& lhs, const Sales_item& rhs) 84 { 85 Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return 86 ret += rhs; // add in the contents of (|rhs|) 87 return ret; // return (|ret|) by value 88 } 89 90 std::istream& 91 operator>>(std::istream& in, Sales_item& s) 92 { 93 double price; 94 in >> s.bookNo >> s.units_sold >> price; 95 // check that the inputs succeeded 96 if (in) 97 s.revenue = s.units_sold * price; 98 else 99 s = Sales_item(); // input failed: reset object to default state 100 return in; 101 } 102 103 std::ostream& 104 operator<<(std::ostream& out, const Sales_item& s) 105 { 106 out << s.isbn() << " " << s.units_sold << " " 107 << s.revenue << " " << s.avg_price(); 108 return out; 109 } 110 111 double Sales_item::avg_price() const 112 { 113 if (units_sold) 114 return revenue/units_sold; 115 else 116 return 0; 117 } 118 #endif