练习14.6
1 #include <iostream> 2 #include <string> 3 #include <memory> 4 #include <vector> 5 #include <algorithm> 6 #include <numeric> 7 8 using namespace std; 9 10 class Sales_data { 11 friend istream& operator >> (istream& is, Sales_data &s); 12 friend ostream& operator<<(ostream& os, Sales_data s); 13 public: 14 Sales_data() : bookNo("") {} 15 Sales_data(string s, unsigned us, double up) : bookNo(s), units_sold(us), units_price(up) {} 16 Sales_data operator+(const Sales_data& s); 17 Sales_data& operator+=(const Sales_data& s); 18 private: 19 string bookNo; 20 unsigned units_sold = 0; 21 double units_price = 0; 22 }; 23 24 istream& operator >> (istream& is, Sales_data &s); 25 ostream& operator<<(ostream& os, Sales_data s); 26 27 int main() 28 { 29 Sales_data s1; 30 Sales_data s2("C++ primer", 10, 128.2); 31 cout << s2 << endl; 32 s1 = s2; 33 cout << s1 << endl; 34 cout << s1 + s2 << endl; 35 s1 += s2; 36 cout << s1 << endl; 37 cin >> s1; 38 cout << s1; 39 cout << s1 + s2 << endl; 40 system("pause"); 41 return 0; 42 } 43 44 istream & operator >> (istream & is, Sales_data &s) 45 { 46 is >> s.bookNo >> s.units_sold >> s.units_price; 47 return is; 48 // TODO: 在此处插入 return 语句 49 } 50 51 ostream & operator<<(ostream & os, Sales_data s) 52 { 53 os << s.bookNo << " " << s.units_sold << " " << s.units_price; 54 return os; 55 // TODO: 在此处插入 return 语句 56 } 57 58 Sales_data & Sales_data::operator+=(const Sales_data & s) 59 { 60 if (bookNo == s.bookNo) 61 { 62 units_sold = units_sold + s.units_sold; 63 return *this; 64 } 65 else 66 { 67 cout << "can not add" << endl; 68 return *this; 69 } 70 // TODO: 在此处插入 return 语句 71 } 72 73 Sales_data Sales_data::operator+(const Sales_data & s) 74 { 75 if (bookNo == s.bookNo) 76 { 77 Sales_data ss; 78 ss.bookNo = bookNo; 79 ss.units_price = units_price; 80 ss.units_sold = units_sold + s.units_sold; 81 return ss; 82 } 83 else 84 { 85 cout << "error" << endl; 86 return *this; 87 } 88 // TODO: 在此处插入 return 语句 89 }
练习14.7
1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <algorithm> 6 7 using namespace std; 8 9 class String { 10 friend ostream &operator<<(ostream &os, String &s); 11 public: 12 String() : element(nullptr), first_free(nullptr) {} 13 String(char *); 14 private: 15 static allocator<char> alloc; 16 char *element; 17 char *first_free; 18 }; 19 allocator<char> String::alloc; 20 ostream &operator<<(ostream &os, String &s); 21 22 int main() 23 { 24 String s1; 25 String s2("hello"); 26 String s3("hello world"); 27 cout << s1; 28 cout << s2; 29 cout << s3; 30 system("pause"); 31 return 0; 32 } 33 34 String::String(char *s) 35 { 36 int i = 0; 37 while (s[i] != ‘\0‘) 38 ++i; 39 auto newloc = alloc.allocate(i); 40 auto dest = newloc; 41 for (auto count = 0; count != i;++count) 42 alloc.construct(dest++, s[count]); 43 element = newloc; 44 first_free = dest; 45 } 46 47 ostream & operator<<(ostream &os, String &s) 48 { 49 while (s.element != s.first_free) 50 { 51 os << *(s.element); 52 s.element++; 53 } 54 cout << endl; 55 return os; 56 // TODO: 在此处插入 return 语句 57 }
练习14.8
类似上题,很简单,细心注意一下就行了
时间: 2024-10-11 19:00:11