设为首页 加入收藏

TOP

1.5 Introducing Classes (2)
2013-10-07 16:14:54 来源: 作者: 【 】 浏览:71
Tags:1.5 Introducing Classes

1.5.1 The Sales_item Class

The purpose of the Sales_item class is to represent the total revenue, number of copies sold, and average sales price for a book. How these data are stored or computed is not our concern. To use a class, we need not care about how it is implemented. Instead, what we need to know is what operations objects of that type can perform.

Every class defines a type. The type name is the same as the name of the class. Hence, our Sales_item class defines a type named Sales_item. Aswith the built-in types, we can define a variable of a class type. When we write

Sales_item item;

we are saying that item is an object of type Sales_item. We often contract the phrase “an object of type Sales_item” to “a Sales_item object” or even more simply to “a Sales_item.”

In addition to being able to define variables of type Sales_item, we can:

.. Call a function named isbn to fetch the ISBN from a Sales_item object.

.. Use the input (>>) and output (<<) operators to read and write objects of type Sales_item.

.. Use the assignment operator (=) to assign one Sales_item object to another.

.. Use the addition operator (+) to add two Sales_item objects. The two objects must refer to the same ISBN. The result is a new Sales_item object whose ISBN is that of its operands and whose number sold and revenue are the sum of the corresponding values in its operands.

.. Use the compound assignment operator (+=) to add one Sales_item object into another.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.4 Flow of Control (5) 下一篇1.5 Introducing Classes (1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)