Sales_data
ClassUnlike 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.
Sales_data
ObjectsBecause 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 program will 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;
// code to read into data1 and data2
// code to check whether data1 and data2 have the same ISBN
// and if so print the sum of data1 and 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
.
Sales_data
ObjectAlthough we won’t describe the library string
type in detail until Chapters 3 and 10, we need to know only a little bit about string
s 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 string
s, respectively. With this knowledge we can write the code to read the first transaction:
double price = 0; // price per book, used to calculate total revenue
// read the first transactions: ISBN, number of books sold, price per book
std::cin >> data1.bookNo >> data1.units_sold >> price;
// calculate total revenue from price and units_sold
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
:
// read the second transaction
std::cin >> data2.bookNo >> data2.units_sold >> price;
data2.revenue = data2.units_sold * price;
Sales_data
ObjectsOur 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;
// print: ISBN, total sold, total revenue, average price per book
std::cout << data1.bookNo << " " << totalCnt
<< " " << totalRevenue << " ";
if (totalCnt != 0)
std::cout << totalRevenue/totalCnt << std::endl;
else
std::cout << "(no sales)" << std::endl;
return 0; // indicate success
} else { // transactions weren't for the same ISBN
std::cerr << "Data must refer to the same ISBN"
<< std::endl;
return -1; // indicate failure
}
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.