Sales_item
ClassThe 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.
Sales_item
sNow 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
.
Sales_item
sA 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 int
s 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.
Exercise 1.20: http://www.informit.com/title/0321714113 contains a copy of Sales_item.h
in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.
Exercise 1.21: Write a program that reads two Sales_item
objects that have the same ISBN and produces their sum.
Exercise 1.22: Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.