设为首页 加入收藏

TOP

1.5 Introducing Classes (6)
2013-10-07 16:14:30 来源: 作者: 【 】 浏览:85
Tags:1.5 Introducing Classes

1.5.2 A First Look at Member Functions

Our program that adds two Sales_items should check whether the objects have the same ISBN. We’ll do so as follows:

  1. #include <iostream>   
  2. #include "Sales_item.h"   
  3. int main()   
  4. {   
  5. Sales_item item1, item2;   
  6. std::cin >> item1 >> item2;   
  7. // first check that item1 and item2 represent the same book   
  8. if (item1.isbn() == item2.isbn()) {   
  9. std::cout << item1 + item2 << std::endl;   
  10. return 0; // indicate success   
  11. else {   
  12. std::cerr << "Data must refer to same ISBN"   
  13. << std::endl;   
  14. return -1; // indicate failure   
  15. }   
  16. }  

The difference between this program and the previous version is the if and its associated else branch. Even without understanding the if condition, we know what this program does. If the condition succeeds, then we write the same output as before and return 0, indicating success. If the condition fails, we execute the block following the else, which prints a message and returns an error indicator.

What Is a Member Function

The if condition

  1. item1.isbn() == item2.isbn()  

calls a member function named isbn. A member function is a function that is defined as part of a class. Member functions are sometimes referred to as methods.

Ordinarily, we call a member function on behalf of an object. For example, the first part of the left-hand operand of the equality expression

  1. item1.isbn  

uses the dot operator (the “.” operator) to say that we want “the isbn member of the object named item1.” The dot operator applies only to objects of class type.
The left-hand operand must be an object of class type, and the right-hand operand must name a member of that type. The result of the dot operator is the member named by the right-hand operand.

When we use the dot operator to access a member function, we usually do so to call that function. We call a function using the call operator (the () operator). The call operator is a pair of parentheses that enclose a (possibly empty) list of arguments. The isbn member function does not take an argument. Thus,

  1. item1.isbn()  

calls the isbn function that is a member of the object named item1. This function returns the ISBN stored in item1.

The right-hand operand of the equality operator executes in the same way—it returns the ISBN stored in item2. If the ISBNs are the same, the condition is true; otherwise it is false.

EXERCISES SECTION 1.5.2

Exercise 1.23: Write a program that reads several transactions and counts how many transactions occur for each ISBN.

Exercise 1.24: Test the previous programby givingmultiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.6 The Bookstore Program (1) 下一篇CHAPTER SUMMARY

评论

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

·数据库:推荐几款 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)