forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex17_4_5_6_7_8_SalesData.cpp
134 lines (115 loc) · 3.4 KB
/
ex17_4_5_6_7_8_SalesData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
*
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/
#include <iostream>
using std::istream; using std::ostream;
#include "ex17_4_5_6_7_8_SalesData.h"
Sales_data::Sales_data(std::istream &is)
{
// read will read a transaction from is into this object
read(is, *this);
}
double
Sales_data::avg_price() const {
if (units_sold)
return revenue/units_sold;
else
return 0;
}
// add the value of the given Sales_data into this object
Sales_data&
Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold; // add the members of rhs into
revenue += rhs.revenue; // the members of ``this'' object
return *this; // return the object on which the function was called
}
// = Sales_data
Sales_data &Sales_data::operator =(const Sales_data &rhs)
{
this->bookNo = rhs.bookNo;
this->revenue = rhs.revenue;
this->units_sold = rhs.units_sold;
return *this;
}
// =string
Sales_data &Sales_data::operator =(const std::string &rhs)
{
*this= Sales_data(rhs);
return *this;
}
// +=
Sales_data &Sales_data::operator +=(const Sales_data &rhs)
{
this->revenue += rhs.revenue;
this->units_sold += rhs.units_sold;
return *this;
}
Sales_data
add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs; // copy data members from lhs into sum
sum.combine(rhs); // add data members from rhs into sum
return sum;
}
// transactions contain ISBN, number of copies sold, and sales price
istream&
read(istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
ostream&
print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
}
// added 10.Jan 2014
std::ostream &
operator <<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
}
// added 12.Jan 2014
std::istream&
operator >>(std::istream &is, Sales_data &s)
{
double price;
// read input
is >> s.bookNo >> s.units_sold >> price;
// if successful, write into the object, give the object default state otherwise.
if(is)
s.revenue = s.units_sold * price;
else
s = Sales_data();
return is;
}