当前位置: 首页 > news >正文

C++ Primer(第5版)- Chapter 7. Classes -005

Using the Revised Sales_data Class

使用改进的Sales_data类

Before we think about how to implement our class, let’s look at how we can use our interface functions. As one example, we can use these functions to write a version of the bookstore program from § 1.6 (p. 24) that works with Sales_data objects rather than Sales_items:

在考虑如何实现我们的类之前,首先来看看应该如何使用上面这些接口函数。举个例子,我们能使用这些函数编写1.6节(第24页)书店程序的另外一个版本,这个程序使用Sales_data对象,不再使用Sales_items对象。

We start by defining a Sales_data object to hold the running total. Inside the if condition, we call read to read the first transaction into total. This condition works like other loops we’ve written that used the >> operator. Like the >> operator, our read function will return its stream parameter, which the condition checks (§ 4.11.2, p. 162). If the read fails, we fall through to the else to print an error message.

一开始我们定义了一个Sales_data对象用于保存实时汇总信息。在 if 条件内部,调用read函数将第一条交易读入到total中。这里的条件部分,与之前我们使用>>运算符的效果是一样的。read函数将返回他的参数,而条件部分负责检查这个返回值(第4.11.2节,第162页)。如果read函数失败,程序将直接跳转到else语句并输出一条错误信息。

If there are data to read, we define trans, which we’ll use to hold each transaction. The condition in the while also checks the stream returned by read. So long as the input operations in read succeed, the condition succeeds and we have another transaction to process.

检测到有数据输入,我们定义变量trans用于保存每一个交易。while语句的条件部分同样会检查read函数的返回值。只要在函数read里的输入操作成功,条件就被满足,意味着我们可以处理一个新的交易。

Inside the while, we call the isbn members of total and trans to fetch their respective ISBNs. If total and trans refer to the same book, we call combine to add the components of trans into the running total in total. If trans represents a new book, we call print to print the total for the previous book. Because print returns a reference to its stream parameter, we can use the result of print as the left-hand operand of the <<. We do so to print a newline following the output generated by print. We next assign trans to total, thus setting up to process the records for the next book in the file.

while循环内部,我们分别调用totaltrans对象的成员函数isbn,来取得各自的ISBNs值。如果totaltrans指示的是同一本书,我们调用combine函数将trans的内容添加到total的实时汇总信息中。如果trans指的是一本新书,我们调用print函数,输出前一本书的汇总信息。因为print函数返回的是它的流参数的引用,所以我们能用print的返回值作为<<运算符的左侧运算对象。通过这种方式,在输出print函数的处理结果后,然后转到下一行。接下来,把trans赋给total,从而为处理文件中下一本书的记录做好了准备。

After we have exhausted the input, we have to remember to print the data for the last transaction, which we do in the call to print following the while loop.

在处理完所有输入数据后,我们必须使用while循环语句后面的print函数,输出最后一个交易的数据信息。

http://www.lryc.cn/news/596923.html

相关文章:

  • ESP32的ADF详解:6. Audio Processing的API
  • Android 测试全指南:单元测试与UI测试框架详解
  • ESP-NOW实战:ESP32一对多无线通信方案(支持ESP8266兼容)
  • 【Spring Cloud Gateway 实战系列】进阶篇:过滤器高级用法、动态路由配置与性能优化
  • Android Studio中调用USB摄像头
  • CRMEB 单商户PRO多商户通用去版权教程
  • win11安装erlang和rabbitmq
  • SpringBoot 使用Rabbitmq
  • RabbitMQ--@RabbitListener及@RabbitHandle
  • OceanBase数据库
  • RabbitMQ有多少种Exchange?
  • LLM 幻觉一般是由于什么产生的,在模型什么部位产生
  • Java学习第六十九部分——RabbitMQ
  • iOS WebView 远程调试实战 解决表单输入被键盘遮挡和焦点丢失问题
  • 期权遇到股票分红会调整价格吗?
  • 【机器学习深度学习】比较 LLaMA-Factory、vLLM 和 LMDeploy 的量化导出:为何 LLaMA-Factory 不是首选?
  • OpenCV(01)基本图像操作、绘制,读取视频
  • Redis MCP 安装与配置完整指南
  • Spring Boot全局异常处理:一网打尽Controller层异常,@RestControllerAdvice解析
  • Unreal5从入门到精通之使用 Python 编写虚幻编辑器脚本
  • Linux进程控制:掌握系统的核心脉络
  • 《设计模式之禅》笔记摘录 - 9.责任链模式
  • Xorg占用显卡内存问题和编译opencv GPU版本
  • 基于LNMP分布式个人云存储
  • Docker 容器中的 HEAD 请求缺失 header?从 Content-MD5 缺失聊起
  • BitDistiller:通过自蒸馏释放 Sub-4-Bit 大语言模型的潜力
  • BiLLM:突破大语言模型后训练量化的极限
  • AI安全“面壁计划”:我们如何对抗算法时代的“智子”封锁?
  • 主要分布在背侧海马体(dHPC)CA1区域(dCA1)的时间细胞对NLP中的深层语义分析的积极影响和启示
  • 使用 QLExpress 构建灵活可扩展的业务规则引擎