/*
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. As with 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.
Reading and Writing Sales_items
Now that we know what operations we can use with Sales_item objects, we can write programs
that use the class. For example, the following program reads data from the standard input into
a Sales_item object and writes that Sales_item back onto the standard output:
*/
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book; // read ISBN, number of copies sold, and sales price
std::cin >> book; // write ISBN, number of copies sold, total revenue, and average price
std::cout << book << std::endl;
return 0;
}
/*
If the input to this program is
0-201-70353-X 4 24.99
then the output will be
0-201-70353-X 4 99.96 24.99
Our input says that we sold four copies of the book at $24.99 each, and the output indicates
that the total sold was four, the total revenue was $99.96, and the average price per book was
$24.99.
This program starts with two #include directives, one of which uses a new form. Headers from
the standard library are enclosed in angle brackets (< >). Those that are not part of the
library are enclosed in double quotes (" ").
Inside main we define an object, named book, that we’ll use to hold the data that we read from
the standard input. The next statement reads into that object, and the third statement prints
it to the standard output followed by printing endl.
Adding Sales_items
A more interesting example adds two Sales_item objects:
*/
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2; // read a pair of transactions
std::cout << item1 + item2 << std::endl; // print their sum
return 0;
}
/*
If we give this program the following input
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
our output is
0-201-78345-X 5 110 22
This program starts by including the Sales_item and iostream headers. Next we define two
Sales_item objects to hold the transactions. We read data into these objects from the standard
input. The output expression does the addition and prints the result.
It’s worth noting how similar this program looks to the one on page 6: We read two inputs and
write their sum. What makes this similarity noteworthy is that instead of reading and printing
the sum of two integers, we’re reading and printing the sum of two Sales_item objects.
Moreover, the whole idea of “sum” is different. In the case of ints we are generating a
conventional sum—the result of adding two numeric values. In the case of Sales_item objects we
use a conceptually new meaning for sum—the result of adding the components of two Sales_item
objects.
*/
/*
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 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
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
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,
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.
*/