我是靠谱客的博主 朴实小蝴蝶,这篇文章主要介绍7.1节练习,现在分享给大家,希望可以做个参考。

学习第十二章前,再温习一下第七章。( = 。= 忘得差不多了

7.1 使用2.6.1节定义的Sales_data类为1.6节(第21页)的交易处理程序编写一个新版本。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream> #include "Sales_data.h" #include <string> using namespace std; int main() { Sales_data total; //输入三个数据 book_no, book_sold, revenue. if (cin >> total.book_no >> total.book_sold >> total.revenue) { Sales_data trans; while (cin >> trans.book_no >> trans.book_sold >> trans.revenue) { if (total.book_no == trans.book_no) { total.book_sold += trans.book_sold; total.revenue += trans.revenue; }else{ cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl; total = trans; } } cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl; } else { cerr << "no data."; return -1; } return 0; }


7.2 曾在2.6.2节的练习(第67页)中辫子额了一个Sales_data类,请向这个类添加combine和isbn成员。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Sales_data { Sales_data & combine(Sales_data &); std::string isbn() const { return book_no }; std::string book_no ; unsigned book_sold = 0; double revenue = 0.0; }; Sales_data & Sales_data::combine(Sales_data & rhs) { book_sold += rhs.book_sold; revenue += rhs.revenue; return *this; }

7.3 修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream> #include "Sales_data.h" #include <string> using namespace std; int main() { Sales_data total; if (cin >> total.book_no >> total.book_sold >> total.revenue) { Sales_data trans; while (cin >> trans.book_no >> trans.book_sold >> trans.revenue) { if (total.isbn() == trans.isbn()) { total.combine(trans); } else { cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl; total = trans; } } cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl; } else { cerr << "no data."; return -1; } return 0; }

练习7.4 编写一个名为Personde 类,使其表示人员的姓名和住址。使用string对象存放这些元素,接下来的练习将部队充实这个类的其他特征。

复制代码
1
2
3
4
5
struct Person { string name; string adress; };

练习7.5 在你的Person类中提供一些操作使其能够返回姓名和住址,这些函数是否应该是const的呢?解释原因。

函数应该是常量成员函数,因为若调用成员函数的对象是一个常量,则会类型不适用。

复制代码
1
2
3
4
5
6
7
struct Person { std::string isname() const { return name; }; std::string isadress() const { return adress; }; std::string name; std::string adress; };

练习7.6 对于函数add、read和print,定义你自己的版本。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
struct Sales_data { Sales_data & combine(Sales_data &); std::string isbn() const { return book_no; }; double avg_price()const; std::string book_no ; unsigned book_sold = 0; double revenue = 0.0; }; double Sales_data::avg_price()const { double avg = 0; if (book_sold) { avg = revenue / book_sold; } return avg; } Sales_data & Sales_data::combine(Sales_data & rhs) { book_sold += rhs.book_sold; revenue += rhs.revenue; return *this; } Sales_data add(const Sales_data &rhs, const Sales_data &lhs) { Sales_data sum = rhs; sum.combine(rhs); return sum; } std::istream &read(std::istream is, Sales_data &item) { double price = 0; is >> item.book_no >> item.book_sold >> price; item.revenue = item.book_sold * price; return is; } std::ostream &print(std::ostream, const Sales_data &item) { os << item.book_no << ": " << item.book_sold << " " << item.revenue << " " << item.avg_price(); return os; }

练习7.8 为什么read函数将其Sales_data参数定义成普通的引用,而print将其参数定义成常量的引用。

因为read函数中修改了对象的数据revenue,而print函数中没有。


练习7.9 对于7.1.2节(第233页)练习中的代码,添加读取和打印Person对象的操作。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> #include <string> struct Person { std::string isname() const { return name; }; std::string isadress() const { return adress; }; std::string name; std::string adress; }; std::istream &read(std::istream &is, Person &m) { is >> m.name >> " " >> m.adress; return is; } std::ostream &print(std::ostream &os, const Person &m) { os << m.isname()<< " " << m.isadress(); return os; }

练习7.10 在下面这条if语句中,条件部分的作用是什么?

if(read((read(cin,data1)),data2))

答:read(cin,data1)返回的是istream& 输入流,

       read((read(cin,data1)),data2)返回的也是istream& 输入流。

所以条件部分检测的是是否将data2的数据输入data1。


练习7.11 在你的Sales_data类中添加构造函数,然后编写一段程序令其用到每个构造函数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> #include <string> #include "Sales_data.h" using namespace std; int main() { Sales_data item1; Sales_data item2("C++primer"); Sales_data item3("CprimerPlus", 2, 40.2); Sales_data item4(cin); print(cout, item1) << endl; print(cout, item2) << endl; print(cout, item3) << endl; print(cout, item4) << endl; }


练习7.12 把只接受一个istream作为参数的构造函数定义移动到类的内部。

复制代码
1
2
3
4
5
6
Sales_data(std::istream &is) { double price = 0; is >> book_no >> book_sold >> price; revenue = price*book_sold; }

7.13 使用istream构造函数重写第229页的程序。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream> #include <string> #include "Sales_data.h" using namespace std; int main() { Sales_data total; if (read(cin,total)) { Sales_data trans; while (read(cin, trans)) { if (total.isbn() == trans.isbn()) { total=add(total, trans); } else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } else { cerr << "no!" << endl; } return 0; }

7.14 编写一个构造函数,令其用我们提供的类内初始值显式地初始化成员。

(把先前版本的接受三参数构造函数屏蔽)

复制代码
1
2
3
4
5
6
7
//Sales_data(std::string s,unsigned n,double p):book_no(s),book_sold(n),revenue(n*p){} Sales_data(std::string s, unsigned n, double p) { book_no = s; book_sold = n; revenue = p*n; }


7.15 为你的Person类添加正确的构造函数。

复制代码
1
2
3
Person() = default Person(std::string n):name(n){} Person(std::string n, std::string a):name(n),adress(a){}


最后

以上就是朴实小蝴蝶最近收集整理的关于7.1节练习的全部内容,更多相关7内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部