https://github.com/jzplp/Cpp-Primer-Answer
-
練習14.1
重載的運算符和內置運算符在對象求值順序和短路求值屬性上不同。
在優先級和結合律,運算對象的數量上相同。 -
練習14.2
Sales_data類 書上的版本
添加了重載的輸入,輸出,加法和複合賦值運算符
14.2 Sales_data.h程序代碼
14.2 Sales_data.cpp程序代碼
14.2 測試程序代碼 -
練習14.3
(a) 使用const char * 的
(b) 使用string的
(c) 使用vector的,在vector內部使用string的
(d) 使用string的 -
練習14.4
(a) 非成員
(b) 成員
(c) 成員
(d) 成員
(e) 非成員
(f) 非成員
(g) 非成員
(h) 成員 -
練習14.5
Tree類 自己的簡單版本(始於7.40題)
增加了重載的==運算符
14.5 Tree.h程序代碼
14.5 測試程序代碼 -
練習14.6
Sales_data類 書上的版本
函數已經在14.2的代碼中實現
14.2 Sales_data.h程序代碼
14.2 Sales_data.cpp程序代碼
14.2 測試程序代碼 -
練習14.7
String類 自己定義 模仿書上的StrVec類
增加了重載的輸出運算符
14.7 String.h 程序代碼
14.7 String.cpp 程序代碼
14.7 測試程序代碼 -
練習14.8
Tree類 自己的簡單版本(始於7.40題)
增加了重載的輸出運算符
14.8 Tree.h程序代碼
14.8 測試程序代碼 -
練習14.9
Sales_data類 書上的版本
更改爲了書上版本的重載輸入運算符
14.9 Sales_data.h程序代碼
14.9 Sales_data.cpp程序代碼
14.9 測試程序代碼 -
練習14.10
(a) 正常讀取
(b) 非正常讀取,把書單價設置爲了0.95
(經測試與答案書上結果不同,.95被成功輸入爲了0.95) -
練習14.11
存在錯誤
(a) 正常讀取
(b) 非正常讀取,把書單價設置爲了0.95 -
練習14.12
Tree類 自己的簡單版本(始於7.40題)
增加了重載的輸入運算符
14.12 Tree.h程序代碼
14.12 測試程序代碼 -
練習14.13
目前沒有想到 -
練習14.14
- 這樣在操作上可行,是可以正常運行的
- +=和+在邏輯上是類似的運算符,具體的操作也是大部分相同的,如果利用+=來定義+,這樣避免了重複定義操作。
-
練習14.15
不應該,因爲樹不應該含有加減乘除等操作 -
練習14.16
Sales_data類 書上的版本(題目未要求)
增加了書上版本的重載相等性運算符
14.16 Sales_data.h程序代碼
14.16 Sales_data.cpp程序代碼
14.16 僅本題測試程序代碼
StrBlob類 增加了重載相等性運算符
14.16 StrBlob.h程序代碼
14.16 StrBlob.cpp程序代碼
14.16 測試程序代碼
StrVec類 書上的版本
增加了重載相等性運算符
14.16 StrVec.h 程序代碼
14.16 StrVec.cpp 程序代碼
14.16 測試程序代碼
String類 自己定義 模仿書上的StrVec類
增加了重載相等性運算符
14.16 String.h 程序代碼
14.16 String.cpp 程序代碼
14.16 測試程序代碼 -
練習14.17
Tree類 自己的簡單版本(始於7.40題)
增加了重載的相等性運算符
14.17 Tree.h程序代碼
14.17 測試程序代碼 -
練習14.18
StrBlob類
增加了重載關係運算符 改寫了重載相等性運算符
14.18 StrBlob.h程序代碼
14.18 StrBlob.cpp程序代碼
14.18 測試程序代碼
StrVec類 書上的版本
增加了重載關係運算符
14.18 StrVec.h 程序代碼
14.18 StrVec.cpp 程序代碼
14.18 測試程序代碼
String類 自己定義 模仿書上的StrVec類
增加了重載關係運算符
14.18 String.h 程序代碼
14.18 String.cpp 程序代碼
14.18 測試程序代碼 -
練習14.19
不應該,因爲樹不應該含有關係運算符 -
練習14.20
Sales_data類 書上的版本
加法運算符已經在之前實現了
14.16 Sales_data.h程序代碼
14.16 Sales_data.cpp程序代碼
14.9 測試程序代碼 -
練習14.21
Sales_data類 本題代碼後續不應採用
較差版本的加法運算符
+運算符執行實際的加法操作,而+=調用+,這樣會造成定義額外的局部變量,造成額外的資源消耗
14.21 Sales_data.h程序代碼
14.21 Sales_data.cpp程序代碼
14.21 測試程序代碼 -
練習14.22
Sales_data類 書上的版本
增加了參數爲string的賦值運算符
14.22 Sales_data.h程序代碼
14.22 Sales_data.cpp程序代碼
14.22 僅本題測試程序代碼 -
練習14.23
StrVec類 書上的版本
增加了重載花括號列表賦值運算符
14.23 StrVec.h 程序代碼
14.23 StrVec.cpp 程序代碼
14.23 測試程序代碼 -
練習14.24
不應該。 -
練習14.25
不應定義其它賦值運算符。(我偷懶了) -
練習14.26
StrBlob類
爲StrBlob類,StrBlobPtr類,ConstStrBlobPtr類增加了下標運算符
14.26 StrBlob.h程序代碼
14.26 StrBlob.cpp程序代碼
14.26 測試程序代碼
StrVec類 書上的版本
增加了下標運算符
14.26 StrVec.h 程序代碼
14.26 StrVec.cpp 程序代碼
14.26 測試程序代碼
String類 自己定義 模仿書上的StrVec類
增加了下標運算符
14.26 String.h 程序代碼
14.26 String.cpp 程序代碼
14.26 測試程序代碼 -
練習14.27
StrBlob類
爲StrBlobPtr類,ConstStrBlobPtr類增加了遞增遞減運算符
14.27 StrBlob.h程序代碼
14.27 StrBlob.cpp程序代碼
14.27 測試程序代碼 -
練習14.28
StrBlob類
爲StrBlobPtr類,ConstStrBlobPtr類增加了各類加減運算符
14.28 StrBlob.h程序代碼
14.28 StrBlob.cpp程序代碼
14.28 測試程序代碼 -
練習14.29
因爲遞增和遞減運算符需要改變類對象,因此const類對象不能進行這種操作 -
練習14.30
StrBlob類
爲StrBlobPtr類,ConstStrBlobPtr類增加了解引用和箭頭運算符
14.30 StrBlob.h程序代碼
14.30 StrBlob.cpp程序代碼
14.30 測試程序代碼 -
練習14.31
StrBlobPtr和ConstStrBlobPtr因爲weak_ptr只是對指針的弱引用,因此直接銷燬不影響指針引用的對象,而且可以隨便複製。 所以拷貝構造函數,拷貝複製運算符,析構函數依賴於合成的版本即可 -
練習14.32
StrBlob類 僅更改了測試代碼
14.32 StrBlob.h程序代碼
14.32 StrBlob.cpp程序代碼
14.32 新建類測試程序代碼 -
練習14.33
有一個默認的類對象this實參,其他的任意 -
練習14.34
重載了函數調用運算符的if-then-else類
14.34 程序代碼 -
練習14.35
自己定義的InputString類 仿照書上的PrintString類
14.35 程序代碼 -
練習14.36
自己定義的InputString類 仿照書上的PrintString類
只更改了測試代碼
14.36 程序代碼 -
練習14.37
檢查相等類
14.37 程序代碼 -
練習14.38
14.38 程序代碼 -
練習14.39
14.39 程序代碼 -
練習14.40
biggies函數 用函數對象類代替lambda
14.40 程序代碼 -
練習14.41
因爲lambda編寫方便,對於小函數很好用
如果需要保存狀態,或者是處理複雜,可以使用函數對象類 -
練習14.42
(a)
std::count_if(v.begin(), v.end(), std::bind(std::greater<int>(), std::placeholders::_1, 1024));
(b)
std::find_not_if(v.begin(), v.end(), std::bind(std::equal_to<std::string>(), std::placeholders::_1, "pooh"));
(c)
std::tranform(v.begin(), v.end(), v.begin(), std::bind(std::multiplies<int>(),std::placeholders::_1, 2));
-
練習14.43
14.43 程序代碼 -
練習14.44
14.44 簡單的二元運算計算器 程序代碼 -
練習14.45
Sales_data類 書上的版本
增加了轉換爲string和double的類型轉換運算符
14.45 Sales_data.h程序代碼
14.45 Sales_data.cpp程序代碼
14.45 僅本題測試程序代碼 -
練習14.46
應該定義,但是必須聲明成explicit的。如果是隱式的,可能會造成意外結果 -
練習14.47
operator const int();
轉換爲const int類型,並且只能是非常量類對象才能使用
operator int() const;
轉換爲int類型,常量和非常量類對象都可以使用
-
練習14.48
可以定義,但是必須是explicit的,否則可能造成意外結果 -
練習14.49
Tree類 自己的簡單版本(始於7.40題)
增加了轉換爲bool的類型轉換運算符
14.49 Tree.h程序代碼
14.49 測試程序代碼 -
練習14.50
ex1: 初始化錯誤
1. LongDouble轉換爲double,double轉換爲int
2. LongDouble轉換爲float,float轉換爲int
ex2: 初始化正確
LongDouble到float
- 練習14.51
可能的類型轉換序列:
1. double轉換爲LongDouble, 調用void calc(LongDouble)
2. double轉換爲int,調用void calc(int)
最佳可行函數爲2,因爲通過算術類型的轉化比類類型轉換的等級要高
- 練習14.52
ld = si + ld;
1. SmallInt轉換爲int,LongDouble轉換爲double,double轉換爲int,調用int的加法
2. SmallInt轉換爲int,LongDouble轉換爲float,float轉換爲int,調用int的加法
3. SmallInt轉換爲int,int轉換爲double,LongDouble轉換爲double,調用double的加法
沒有哪種方式明確的優於另外的幾種,因此會發生錯誤
ld = si + ld;
1. SmallInt轉換爲int,int轉換爲double, 調用LongDoule operator+(LongDouble&, double)
2. LongDouble轉換爲double,SmallInt轉換爲int,int轉換爲double,調用double的加法
3. LongDouble轉換爲double,double轉換爲int,SmallInt轉換爲int,調用int的加法
4. LongDouble轉換爲float,float轉換爲int,SmallInt轉換爲int,調用int的加法
5. 調用LongDouble LongDouble::operator(const SmallInt&)
第5種不需要轉換,優於其他幾種
- 練習14.53
不合法,會產生二義性
1. s1轉換爲int,int轉換爲double,調用double的加法
2. double轉換爲int,int轉換爲SmallInt,調用SmallInt SmallInt::operator(const SmallInt&, const SmallInt&)
修改的方式有很多種,其中一種爲:
double d = int(s1) + 3.14;