Sales_data.h

 1 #ifndef SALES_DATA_H
2 #define SALES_DATA_H
3
4 #include "Version_test.h"
5
6 #include <string>
7 #include <iostream>
8
9 class Sales_data {
10 friend Sales_data add(const Sales_data&, const Sales_data&);
11 friend std::ostream &print(std::ostream&, const Sales_data&);
12 friend std::istream &read(std::istream&, Sales_data&);
13 public:
14 // constructors
15 // using the synthesized version is safe only
16 // if we can also use in-class initializers
17 #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
18 Sales_data() = default;
19 #else
20 Sales_data(): units_sold(0), revenue(0.0) { }
21 #endif
22 #ifdef IN_CLASS_INITS
23 Sales_data(const std::string &s): bookNo(s) { }
24 #else
25 Sales_data(const std::string &s):
26 bookNo(s), units_sold(0), revenue(0.0) { }
27 #endif
28 Sales_data(const std::string &s, unsigned n, double p):
29 bookNo(s), units_sold(n), revenue(p*n) { }
30 Sales_data(std::istream &);
31
32 // operations on Sales_data objects
33 std::string isbn() const { return bookNo; }
34 Sales_data& combine(const Sales_data&);
35 double avg_price() const;
36 private:
37 std::string bookNo;
38 #ifdef IN_CLASS_INITS // using the synthesized version is safe only
39 unsigned units_sold = 0;
40 double revenue = 0.0;
41 #else
42 unsigned units_sold;
43 double revenue;
44 #endif
45 };
46
47
48 // nonmember Sales_data interface functions
49 Sales_data add(const Sales_data&, const Sales_data&);
50 std::ostream &print(std::ostream&, const Sales_data&);
51 std::istream &read(std::istream&, Sales_data&);
52
53 // used in future chapters
54 inline
55 bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
56 {
57 return lhs.isbn() < rhs.isbn();
58 }
59 #endif
  • 成员函数通过隐式this指针访问调用它的对象
  • 常量成员函数不可修改调用对象的内容
  • 通过非成员函数访问类的私有成员,需在类内将此函数声明为友元(友元声明不是函数声明,函数声明在类外)
  • 经常使用的短函数声明为内联函数并在.h文件中定义,类内定义的函数是隐式内联的
  • 构造函数没有返回值,不能被声明为const

Sales_data.cpp

 1 #include <iostream>
2 using std::istream; using std::ostream;
3
4 #include "Sales_data.h"
5 Sales_data::Sales_data(std::istream &is)
6 {
7 // read will read a transaction from is into this object
8 read(is, *this);
9 }
10
11 double
12 Sales_data::avg_price() const {
13 if (units_sold)
14 return revenue/units_sold;
15 else
16 return 0;
17 }
18
19 // add the value of the given Sales_data into this object
20 Sales_data&
21 Sales_data::combine(const Sales_data &rhs)
22 {
23 units_sold += rhs.units_sold; // add the members of rhs into
24 revenue += rhs.revenue; // the members of ``this'' object
25 return *this; // return the object on which the function was called
26 }
27
28 Sales_data
29 add(const Sales_data &lhs, const Sales_data &rhs)
30 {
31 Sales_data sum = lhs; // copy data members from lhs into sum
32 sum.combine(rhs); // add data members from rhs into sum
33 return sum;
34 }
35
36 // transactions contain ISBN, number of copies sold, and sales price
37 istream&
38 read(istream &is, Sales_data &item)
39 {
40 double price = 0;
41 is >> item.bookNo >> item.units_sold >> price;
42 item.revenue = price * item.units_sold;
43 return is;
44 }
45
46 ostream&
47 print(ostream &os, const Sales_data &item)
48 {
49 os << item.isbn() << " " << item.units_sold << " "
50 << item.revenue << " " << item.avg_price();
51 return os;
52 }

avg_price.cpp

 1 #include <iostream>
2 using std::cerr; using std::cin; using std::cout; using std::endl;
3
4 #include "Sales_data.h"
5
6 int main()
7 {
8 Sales_data total; // variable to hold the running sum
9 if (read(cin, total)) { // read the first transaction
10 Sales_data trans; // variable to hold data for the next transaction
11 while(read(cin, trans)) { // read the remaining transactions
12 if (total.isbn() == trans.isbn()) // check the isbns
13 total.combine(trans); // update the running total
14 else {
15 print(cout, total) << endl; // print the results
16 total = trans; // process the next book
17 }
18 }
19 print(cout, total) << endl; // print the last transaction
20 } else { // there was no input
21 cerr << "No data?!" << endl; // notify the user
22 }
23
24 return 0;
25 }

[笔记] 《c++ primer》书店程序 Chapter7的更多相关文章

  1. C++primer书店程序

    #include <iostream> #include <string> #include <cassert> #include <algorithm> ...

  2. [笔记] 《c++ primer》书店程序 Chapter 1

    书店程序是<c++ primer>中重要的实例,涉及大部分重要知识点,但代码分散阅读不便,下面按照章节顺序总结 Sales_item.h #ifndef SALESITEM_H // we ...

  3. Lua学习笔记4. coroutine协同程序和文件I/O、错误处理

    Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...

  4. 微信小程序开发:学习笔记[7]——理解小程序的宿主环境

    微信小程序开发:学习笔记[7]——理解小程序的宿主环境 渲染层与逻辑层 小程序的运行环境分成渲染层和逻辑层. 程序构造器

  5. Linux进程线程学习笔记:运行新程序

    Linux进程线程学习笔记:运行新程序                                         周银辉 在上一篇中我们说到,当启动一个新进程以后,新进程会复制父进程的大部份上下 ...

  6. 个人学习笔记:C语言程序结构

    个人笔记:C语言程序 函数 语句 输入输出对象 标识符 关键字 函数 一个C语言源程序,是由一个或多个函数定义顺序组成的,其中必须有一个函数名为main的主函数.C语言源程序中的函数是指完成特定数据处 ...

  7. [笔记] 《c++ primer》显示器程序 Chapter7

    补充Sales_data没有体现出的其他类特性 Screen.h 1 #include <string> 2 #include <iostream> 3 4 class Scr ...

  8. [笔记] 《c++ primer》书店程序 Chapter2

    Sales_data.h 1 #ifndef SALES_DATA_H 2 #define SALES_DATA_H 3 4 #include "Version_test.h" 5 ...

  9. C++ Primer 学习笔记_88_用于大型程序的工具 --异常处理[续1]

    用于大型程序的工具 --异常处理[续1] 四.又一次抛出 有可能单个catch不能全然处理一个异常.在进行了一些校正行动之后,catch可能确定该异常必须由函数调用链中更上层的函数来处理,catch能 ...

随机推荐

  1. 如何在CMDB中落地应用的概念?

    如何在CMDB中落地应用的概念? 我们前面讲了应用是整个微服务架构体系下运维的核心,而CMDB又是整个运维平台的基石.今天我就讲讲在CMDB中如何落地应用这个核心概念,以及如何建立应用集群分组的思路. ...

  2. [状压DP]吃奶酪

    吃 奶 酪 吃奶酪 吃奶酪 题目描述 房间里放着 n n n 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 ( 0 , 0 ) (0,0) (0,0)点处. 输入 第一行有一个整 ...

  3. 答应我,别在go项目中用init()了

    前言 go的 init函数给人的感觉怪怪的,我想不明白聪明的 google团队为何要设计出这么一个"鸡肋"的机制.实际编码中,我主张尽量不要使用init函数. 首先来看看 init ...

  4. Java基础 Java-IO流 深入浅出

    建议阅读 重要性由高到低 Java基础-3 吃透Java IO:字节流.字符流.缓冲流 廖雪峰Java IO Java-IO流 JAVA设计模式初探之装饰者模式 为什么我觉得 Java 的 IO 很复 ...

  5. 浅谈Android中的事件分发机制

    View事件分发机制的本质就是就是MotionEvent事件的分发过程,即MotionEvent产生后是怎样在View之间传递及处理的. 首先介绍一下什么是MotionEvent.所谓MotionEv ...

  6. 后续来啦:Winform/WPF中快速搭建日志面板

    后续来啦:Winform/WPF中快速搭建日志面板 继昨天发文ASP.NET Core 可视化日志组件使用(阅读文章,查看视频)后,视频下有朋友留言 "Winform客户端的程序能用它不?& ...

  7. 02 . MongoDB复制集,分片集,备份与恢复

    复制集 MongoDB复制集RS(ReplicationSet): 基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB)Paxos(mysql MGR 用的是变种)) 如果发生主 ...

  8. manjaro找不到默认键盘布局

    1 问题描述 manjaro安装fcitx后,没有默认的键盘布局,不是这样: 而是: 2 解决方案 解决方案在启动fcitx时就已经有提示了: 缺少了libjson-c这个库,直接使用pacman搜索 ...

  9. JAVAEE_Servlet_17_关于乱码问题

    关于乱码问题 * 数据传递过程中的乱码 解释:数据传递过程中的乱码是指: 将数据从浏览器发送给服务器的时候,服务器接收到的数据是乱码的. - ISO-8859-1 是国际标准码,不支持中文编码,它兼容 ...

  10. 10行C++代码实现高性能HTTP服务

    前言 是不是觉得C++写个服务太累,但又沉迷于C++的真香性能而无法自拔?作为一个老牌C++程序员(可以看我 github 上十几年前的C++项目:https://github.com/kevwan ...