C++ Primer 7.33 练习编写成员函数
来源:互联网 发布:日本动画公司排行知乎 编辑:程序博客网 时间:2024/06/18 16:26
这里我们编写一个成员函数,可以实现两个sales_item相加。实现起来如下:
1.先在Sales_item.h中类定义里添加声明。
Sales_item add(Sales_item& other);
2.再到Sales_item.cpp中实现该函数,记住函数名前要添加域Sales_item::。
//两组交易相加Sales_item Sales_item::add(Sales_item& other){ units_sold += other.units_sold; revenue += other.revenue; return *this;}
3.为了测试效果,我们在主程序里编写如下代码:
//7-33.cpp//读入一组交易,输出每本书的销售册数 总销售收入 和 平均销售价格#include <iostream>#include <Sales_item.h>using namespace std;int main(){ Sales_item total, trans; //保存总和和下一笔交易 cout << "Enter some transactions(Ctrl + Z to end):" << endl; if (total.input(cin)) //读入第一个记录有效 { while (trans.input(cin)) //读入后续的交易 if (total.same_isbn(trans)) //新读入的交易有相同isbn则相加 total.add(trans); else //不同则输出 total 并重置total { total.output(cout) << endl;; total = trans; } //输出最后一个total total.output(cout) << endl;; } else { cout << "No data?!" << endl; return -1; } return 0;}
0 0
- C++ Primer 7.33 练习编写成员函数
- 第二遍C++primer->const成员函数
- 第九章 C函数练习-C primer plus
- C++Primer 练习 12.7
- c++primer练习10.9
- c++primer练习10.11
- c++primer练习10.13
- c++primer练习10.14
- c++primer练习10.15
- c++primer练习10.18
- c++primer 练习11.9
- c++primer练习11.33
- c++Primer 练习2.41
- C Primer Plus 第9章 函数 编程练习
- C++Primer中文版中的练习
- C++primer 文本查询练习
- C++Primer练习3.13疑问?
- c primer 8.1练习答案
- 堆 的取最值删除操作和插入操作
- Cocos2d-x对精灵的优化
- 如何将鼠标箭头设置成手型
- Redis,MemCached,MongoDB 概述
- 最美不过少年时,红尘走马,步步相随
- C++ Primer 7.33 练习编写成员函数
- c++ 时间类型详解 time_t
- 剪时光碎片,共度人间烟雨
- AttributeError: Values instance has no attribute 'settings'
- json教程从入门到使用
- Linux命令:lsof命令
- 传感器和骁龙SDK:抢先发布产品
- 编译原理
- 求最长回文串 O(n)的manacher算法