2.6.2 Using the Sales_data Class
Unlike the Sales_item class, our Sales_data class does not provide any operations. Users of Sales_data have to write whatever operations they need. As an example, we’ll write a version of the program from § 1.5.2 (p. 23) that printed the sum of two transactions. The input to our program will be transactions such as
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
Each transaction holds an ISBN, the count of how many books were sold, and the
price at which each book was sold.
Adding Two Sales_data Objects
Because Sales_data provides no operations, we will have to write our own code to do the input, output, and addition operations. We’ll assume that our Sales_data class is defined inside Sales_data.h. We’ll see how to define this header in § 2.6.3 (p. 76).
Because this programwill be longer than any we’ve written so far, we’ll explain it in separate parts. Overall, our program will have the following structure:
- #include <iostream>
- #include <string>
- #include "Sales_data.h"
- int main()
- {
- Sales_data data1, data2;
-
-
-
- }
As in our original program, we begin by including the headers we’ll need and define variables to hold the input. Note that unlike the Sales_item version, our new program includes the string header. We need that header because our code will have to manage the bookNo member, which has type string.
Reading Data into a Sales_data Object
Although we won’t describe the library string type in detail until Chapters 3 and 10, we need to know only a little bit about strings in order to define and use our ISBN member. The string type holds a sequence of characters. Its operations include the >>, <<, and == operators to read,write, and compare strings, respectively. With this knowledge we can write the code to read the first transaction:
- double price = 0;
-
- std::cin >> data1.bookNo >> data1.units_sold >> price;
-
- data1.revenue = data1.units_sold * price;
Our transactions contain the price at which each book was sold but our data structure stores the total revenue. We’ll read the transaction data into a double named price, from which we’ll calculate the revenue member. The input statement
- std::cin >> data1.bookNo >> data1.units_sold >> price;
uses the dot operator (§ 1.5.2, p. 23) to read into the bookNo and units_sold members of the object named data1.
The last statement assigns the product of data1.units_sold and price into
the revenue member of data1.
Our program will next repeat the same code to read data into data2:
-
- std::cin >> data2.bookNo >> data2.units_sold >> price;
- data2.revenue = data2.units_sold * price;
Printing the Sum of Two Sales_data Objects
Our other task is to check that the transactions are for the same ISBN. If so, we’ll print their sum, otherwise, we’ll print an error message:
- if (data1.bookNo == data2.bookNo) {
- unsigned totalCnt = data1.units_sold + data2.units_sold;
- double totalRevenue = data1.revenue + data2.revenue;
-
- std::cout << data1.bookNo << " " << totalCnt
- << " " << totalRevenue << " ";
- if (totalCnt != 0)
- std::cout << totalRevenue/totalCnt << std::endl;
- else
- std::cout << "(no sales)" << std::endl;
- return 0;
- } else {
- std::cerr << "Data must refer to the same ISBN"
- << std::endl;
- return -1;
- }
In the first if we compare the bookNo members of data1 and data2. If those members are the same ISBN, we execute the code inside the curly braces. That code adds the components of our two variables. Because we’ll need to print the average price, we start by computing the total of units_sold and revenue and store those in totalCnt and totalRevenue, respectively. We print those values. Next we check that there were books sold and, if so, print the computed average price per book. If there were no sales, we print a message noting that fact.
EXERCISES SECTION 2.6.2
Exercise 2.41: Use your Sales_data class to rewrite the exercises in § 1.5.1 (p. 22), § 1.5.2 (p. 24), and § 1.6 (p. 25). For now, you should define your Sales_data class in the same file as your main function.