-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ex17_4_5_6_7_8_SalesData.h
119 lines (98 loc) · 3.46 KB
/
ex17_4_5_6_7_8_SalesData.h
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
/*
* 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
*/
//
// Exercise 14.45:
// Write conversion operators to convert a Sales_data to string and to double.
// What values do you think these operators should return?
//
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
#include <iostream>
class Sales_data
{
// friends
friend Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs);
friend std::ostream&
operator << (std::ostream& os, const Sales_data& s);
friend std::istream&
operator >> (std::istream& is, Sales_data& s);
friend Sales_data add(const Sales_data&, const Sales_data&);
friend std::ostream &print(std::ostream&, const Sales_data&);
friend std::istream &read(std::istream&, Sales_data&);
public:
// constructors
Sales_data() = default;
Sales_data(const std::string &s): bookNo(s) { }
Sales_data(const std::string &s, unsigned n, double p):
bookNo(s), units_sold(n), revenue(p*n) { }
Sales_data(const Sales_data &s ):
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
{ }
Sales_data(Sales_data&& s):
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
{ }
~Sales_data(){ }
Sales_data(std::istream &);
std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&);
// assignments
Sales_data& operator =(const Sales_data& rhs);
Sales_data& operator =(const std::string& rhs);
Sales_data& operator +=(const Sales_data& rhs);
// conversion
explicit operator std::string () const { return bookNo; }
explicit operator double () const { return revenue; }
double avg_price() const;
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
// overloaded operators added 10.Jan.2014 for ex14.2
inline Sales_data
operator+(const Sales_data& lhs, const Sales_data& rhs)
{
Sales_data sum = lhs;
sum += rhs;
return sum;
}
std::ostream&
operator << (std::ostream& os, const Sales_data& item);
std::istream&
operator >> (std::istream& is, Sales_data& s);
// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);
// used in future chapters
inline
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
return lhs.isbn() < rhs.isbn();
}
#endif