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:

#include <iostream>#include "Sales_item.h"int main(){    Sales_item item1, item2;    std::cin >> item1 >> item2;    // first check that item1 and item2 represent the same book    if (item1.isbn() == item2.isbn()) {        std::cout << item1 + item2 << std::endl;        return 0;   // indicate success    } else {        std::cerr << "Data must refer to same ISBN"                  << std::endl;        return -1;  // indicate failure    }}

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 ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.