设为首页 加入收藏

TOP

2.8 Class Types (1)
2013-10-07 15:25:19 来源: 作者: 【 】 浏览:66
Tags:2.8 Class Types

In C++(www.cppentry.com) we define our own data types by defining a class. A class defines the data that an object of its type contains and the operations that can be executed by objects of that type. The library types string, istream, and ostream are all defined as
classes.

类(class)是C++(www.cppentry.com) 的重要元素。本节只简单介绍类的数据成员的写法,完整的介绍见第12 章。

C++(www.cppentry.com) support for classes is extensive—in fact, defining classes is so important that we shall devote Parts III through V to describing C++(www.cppentry.com) support for classes and operations using class types.

In Chapter 1 we used the Sales_item type to solve our bookstore problem. We used objects of type Sales_item to keep track of sales data associated with a particular ISBN. In this section, we’ll take a first look at how a simple class, such as Sales_item, might be defined.

Class Design Starts with the Operations

Each class defines an interface and implementation. The interface consists of the operations that we expect code that uses the class to execute. The implementation typically includes the data needed by the class. The implementation also includes any functions needed to define the class but that are not intended for general use.

When we define a class, we usually begin by defining its interface—the operations that the class will provide. From those operations we can then determine what data the class will require to accomplish its tasks and whether it will need to define any functions to support the implementation.

The operations our type will support are the operations we used in Chapter 1. These operations were outlined in Section 1.5.1 (p. 21):

· The addition operator to add two Sales_items

· The input and output operators to read and write Sales_item objects

· The assignment operator to assign one Sales_item object to another

· The same_isbn function to determine if two objects refer to the same book

We’ll see how to define these operations in Chapters 7 and 14 afterwe learn how to define functions and operators. Even though we can’t yet implement these functions, we can figure out what data they’ll need by thinking a bit about what these
operations must do. Our Sales_item class must

1. Keep track of how many copies of a particular book were sold

2. Report the total revenue for that book

3. Calculate the average sales price for that book

Looking at this list of tasks, we can see that we’ll need an unsigned to keep track
of how many books are sold and a double to keep track of the total revenue. From these data we can calculate the average sales price as total revenue divided by number sold. Because we also want to know which book we’re reporting on, we’ll also need a string to keep track of the ISBN.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.8 Class Types (2) 下一篇2.4 const Qualifier (3)

评论

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

·MySQL 安装及连接-腾 (2025-12-25 06:20:28)
·MySQL的下载、安装、 (2025-12-25 06:20:26)
·MySQL 中文网:探索 (2025-12-25 06:20:23)
·Shell脚本:Linux Sh (2025-12-25 05:50:11)
·VMware虚拟机安装Lin (2025-12-25 05:50:08)