C++Primer第五版 7.1.2节练习
2015-09-16 07:16
423 查看
练习7.2:曾在2.6.2节练习(第67页)中编写了一个Sales_data类,请向这个类添加combine和isbn成员。
练习7.3:修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。
练习7.4:编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
答:练习7.2-7.3见云盘程序。
练习7.5:在你的Person类中提供一些操作使其能够返回姓名和住址。这些函数是否应该是const的呢?解释原因。
答:是const的,原因是this指针指向的姓名和地址的存储地址不变,即this指针一直指向所指的对象。
练习7.2
练习7.3
练习7.4
练习7.5
练习7.3:修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。
练习7.4:编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
答:练习7.2-7.3见云盘程序。
练习7.5:在你的Person类中提供一些操作使其能够返回姓名和住址。这些函数是否应该是const的呢?解释原因。
答:是const的,原因是this指针指向的姓名和地址的存储地址不变,即this指针一直指向所指的对象。
练习7.2
/* *练习7.2 *日期:2015/7/1 *问题描述:练习7.2:曾在2.6.2节练习(第67页)中编写了一个Sales_data类,请向这个类添加combine和isbn成员。 *功能;写一个类 *作者:Nick Feng *邮箱:nickgreen23@163.com */ #include <iostream> #include <string> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = 0; double revenue = 0.0; string isbn() const {return bookNo;} Sales_data& combine(const Sales_data&); double avg_price() const; }; double Sales_data::avg_price() const { if(units_sold) return revenue/units_sold; else return 0; } Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; //把rhs的成员加到this对象的成员上 revenue += rhs.revenue; return *this; //返回调用该函数的对象 } int main() { return 0; }
练习7.3
/* *练习7.3 *日期:2015/7/1 *问题描述:练习7.3:修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。 *说明;229页的函数中,功能都已经实现了,而我写的这个Sales_data类还不全,因此只是为了在主函数里面调用这些方法,才进行了改写,写得不好 *作者:Nick Feng *邮箱:nickgreen23@163.com */ #include <iostream> #include <string> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = 0; double revenue = 0.0; string isbn() const {return bookNo;} Sales_data& combine(const Sales_data&); double avg_price() const; }; double Sales_data::avg_price() const { if(units_sold) return revenue/units_sold; else return 0; } Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; //把rhs的成员加到this对象的成员上 revenue += rhs.revenue; return *this; //返回调用该函数的对象 } int main() { Sales_data total; total.bookNo = "123"; total.units_sold = 10; total.revenue = 5; if(total.bookNo != "") { Sales_data trans; trans.bookNo = "123"; trans.units_sold = 10; trans.revenue = 5; if(total.isbn() == trans.isbn()) total.combine(trans); else { cout << total.units_sold << endl; total.bookNo = trans.bookNo; total.units_sold = total.units_sold; total.revenue = total.revenue; } cout << total.units_sold << endl; }else { cerr << "No data?" << endl; } return 0; }
练习7.4
/* *练习7.4 *日期:2015/7/1 *问题描述:练习7.4:编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。 *功能;正式开始写一个类啦,是不是很激动咧 *作者:Nick Feng *邮箱:nickgreen23@163.com */ #include <iostream> #include <string> using namespace std; struct Person{ string name; string address; };
练习7.5
#include <iostream> #include <string> using namespace std; struct Person{ string name; string address; string getName() const {return name;} string getAddress() const {return address;} };
相关文章推荐
- C++Primer第五版 7.1.1节练习
- C++Primer第五版 第6章 函数(练习解答)
- C++Primer第五版 6.7节练习
- C++Primer第五版 6.6节练习
- C++Primer第五版 6.5.3节练习
- C++Primer第五版 6.5.2节练习
- Leetcode Maximal Square
- C语言之赋值操作符和表达式
- C语言中联合体union妙用
- C++那些细节--typedef关键字
- C语言之位操作符
- 配置vim编写C/C++程序时自动缩进
- c++程序设计梳理(谭浩强)3-4章
- c++程序设计梳理(谭浩强)7-8章
- c++程序设计梳理(谭浩强)9章
- c++程序设计梳理(谭浩强)5-6章
- c++程序设计梳理(谭浩强)13章
- 讲解C++中的枚举类型以及声明新类型的方法
- c++主函数输出圆柱体的半径和高,然后调用函数输出其体积
- 链表java_类c++,linkedlist,iterator