设为首页 加入收藏

TOP

2.6 Defining Our Own Data Structures (1)
2013-10-07 16:19:32 来源: 作者: 【 】 浏览:80
Tags:2.6 Defining Our Own Data Structures

At the most basic level, a data structure is a way to group together related data elements and a strategy for using those data. As one example, our Sales_item class groups an ISBN, a count of how many copies of that book had been sold, and the revenue associated with those sales. It also provides a set of operations such as the isbn function and the >>, <<, +, and += operators.

In C++(www.cppentry.com) we define our own data types by defining a class. The library types string, istream, and ostream are all defined as classes, as is the Sales_item type we used in Chapter 1. C++(www.cppentry.com) support for classes is extensive—in fact, Parts III and IV are largely devoted to describing class-related features. Even though the Sales_item class is pretty simple, we won’t be able to fully define that class until we learn how to write our own operators in Chapter 14.

2.6.1 Defining the Sales_data Type

Although we can’t yet write our Sales_item class, we can write a more concrete class that groups the same data elements. Our strategy for using this class is that users will be able to access the data elements directly and must implement needed operations for themselves.

Because our data structure does not support any operations, we’ll name our version Sales_data to distinguish it from Sales_item. We’ll define our class as follows:

  1. struct Sales_data {   
  2. std::string bookNo;   
  3. unsigned units_sold = 0;   
  4. double revenue = 0.0;   
  5. }; 

Our class begins with the keyword struct, followed by the name of the class and a (possibly empty) class body. The class body is surrounded by curly braces and forms a new scope (§ 2.2.4, p. 48). The names defined inside the class must be unique within the class but can reuse names defined outside the class.

The close curly that ends the class body must be followed by a semicolon. The semicolon is needed because we can define variables after the class body:

  1. struct Sales_data { /* ... */ } accum, trans, *salesptr;   
  2. // equivalent, but better way to define these objects   
  3. struct Sales_data { /* ... */ };   
  4. Sales_data accum, trans, *salesptr;  

The semicolon marks the end of the (usually empty) list of declarators. Ordinarily, it is a bad idea to define an object as part of a class definition. Doing so obscures the code by combining the definitions of two different entities—the class and a variable—in a single statement.

It is a commonmistake among newprogrammers to forget the semicolon at the end of a class definition.

Class Data Members

The class body defines the members of the class. Our class has only data members. The data members of a class define the contents of the objects of that class type. Each object has its own copy of the class data members. Modifying the data members of one object does not change the data in any other Sales_data object.

We define data members the same way that we define normal variables: We specify a base type followed by a list of one or more declarators. Our class has three data members: a member of type string named bookNo, an unsigned member named units_sold, and a member of type double named revenue. Each Sales_data object will have these three data members.

Under the new standard, we can supply an in-class initializer for a data member. When we create objects, the in-class initializers will be used to initialize the data members. Members without an initializer are default initialized (§ 2.2.1, p. 43). Thus, when we define Sales_data objects, units_sold and revenue will be initialized to 0, and bookNo will be initialized to the empty string.

In-class initializers are restricted as to the form (§ 2.2.1, p. 43) we can use: They must either be enclosed inside curly braces or follow an = sign. We may not specify an in-class initializer inside parentheses.

In § 7.2 (p. 268), we’ll see that C++(www.cppentry.com) has a second keyword, class, that can be used to define our own data structures. We’ll explain in that section why we use struct here. Until we cover additional class-related features in Chapter 7, you should use struct to define your own data structures.


EXERCISES SECTION 2.6.1

Exercise 2.39: Compile the following program to see what happens when you forget the semicolon after a class definition. Remember the message for future reference.

  1. struct Foo { /* empty */ } // Note: no semicolon   
  2. int main()   
  3. {   
  4. return 0;   
  5. }  

Exercise 2.40: Write your own version of the Sales_data class.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.4 读者-写者问题 下一篇2.6 Defining Our Own Data Struc..

评论

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

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)