7.1.1. Designing the Sales_data Class
Ultimately, we want Sales_data to support the same set of operations as the Sales_item class. The Sales_item class had one member function (§ 1.5.2, p. 23), named isbn, and supported the +,=, +=, <<, and >> operators.
7.1.1 设计Sales_data类
我们的最终目的是令Sales_data类支持与Sales_item类完全一样的操作集合。Sales_item类有一个名为isbn的成员函数(member function)(参见1.5.2节,第23页),并且支持+,=, +=, <<,>>运算符操作。
We’ll learn how to define our own operators in Chapter 14. For now, we’ll define ordinary (named) functions for these operations. For reasons that we will explain in § 14.1 (p. 555), the functions that do addition and IO will not be members of Sales_data. Instead, we’ll define those functions as ordinary functions. The function that handles compound assignment will be a member, and for reasons we’ll explain in § 7.1.5 (p. 267), our class doesn’t need to define assignment.
我们将在第14章学习如何自定义运算符(operators)。现在,我们先为这些运算定义普通的(命名的named)函数形式。由于在第14.1节(第555页)将要解释的原因,执行加法(do addition)和IO的函数不作为Sales_data的成员。相反地,我们将其定义成普通函数(ordinary functions)。执行复合赋值运算(handles compound assignment)的函数是成员函数。我们的类不需要定义赋值运算,其原因将在7.1.5节(第267页)中介绍。
Thus, the interface to Sales_data consists of the following operations:
• An isbn member function to return the object’s ISBN
• A combine member function to add one Sales_data object into another
• A function named add to add two Sales_data objects
• A read function to read data from an istream into a Sales_data object
• A print function to print the value of a Sales_data object on an ostream
综上所述,Sales_data的接口包含(consists of)以下操作:
Key Concept: Different Kinds of Programming Roles
关键概念:不同的编程角色
Programmers tend to think about the people who will run their applications as users. Similarly a class designer designs and implements a class for users of that class. In this case, the user is a programmer, not the ultimate user of the application.
程序员常把运行其程序的人称作用户(user)。类似地,类的设计者也是为其用户设计并实现一个类的人。在这种情况下,类的用户是程序员,而非应用程序的最终使用者(the ultimate user)。
When we refer to a user, the context makes it clear which kind of user is meant. If we speak of user code or the user of the Sales_data class, we mean a programmer who is using a class. If we speak of the user of the bookstore application, we mean the manager of the store who is running the application.
当我们提及(refer to)用户一词时,语境(context)决定了是指那种类型的用户。如果我们说用户代码(user code)或者Sales_data类的用户,指的是使用此类的程序员。如果我们说书店(bookstore)应用程序的用户,则意指运行该应用程序的书店管理者。