summaryrefslogtreecommitdiff
path: root/7p49.cpp
blob: 3cf1c09dc66c7fc09f329d304ec0e3e51313d3c5 (plain)
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
#include <iostream>

/*
 *
 * 7.49
 *
 *
 */

struct SalesData;
SalesData add(const SalesData&, const SalesData&);
std::ostream &print(std::ostream&, const SalesData&);
std::istream &read(std::istream&, SalesData&);
struct SalesData {
	SalesData() = default;
	SalesData(const std::string &s): BookNo(s) { } //constructor, bookNo only
	SalesData(const std::string &s, unsigned n, double p): // constructor, both bookno, UnitsSold and Revenue
		BookNo(s), UnitsSold(n), Revenue(p*n) { }
	SalesData(std::istream &sdcin) { read(sdcin, *this); }; // Constructor, but read from istream
	std::string isbn() const { return BookNo; } // member function

	//-------
	SalesData &combine(SalesData);
	//SalesData &combine(SalesData&);
	//SalesData &combine(const SalesData&) const; Does not compile
	//-------

	inline double avg_price() const; // member function, defined elsewhere
	std::string BookNo; // data member
	unsigned int UnitsSold = 0; // data member
	double Revenue = 0.0; // data member
};

inline double SalesData::avg_price() const {

	if (UnitsSold)
		return Revenue / UnitsSold;
	else
		return 0;
}

SalesData& SalesData::combine(SalesData rhs) {

	UnitsSold += rhs.UnitsSold;
	Revenue += rhs.Revenue;
	return *this;
}

/*
SalesData& SalesData::combine(SalesData &rhs) {

	UnitsSold += rhs.UnitsSold;
	Revenue += rhs.Revenue;
	return *this;
}
*/
/*
SalesData& SalesData::combine(const SalesData &rhs) const {

	UnitsSold += rhs.UnitsSold;
	Revenue += rhs.Revenue;
	return *this;
}
*/

std::istream &read(std::istream &is, SalesData &item) { // Not member function

	double price = 0;
	is >> item.BookNo >> item.UnitsSold >> price;
	item.Revenue = price * item.UnitsSold;
	return is;
}

std::ostream &print(std::ostream &os, const SalesData &item) { // Not member function

	os << item.isbn() << " " << item.UnitsSold << " "
	   << item.Revenue << " " << item.avg_price();
	return os;
}

SalesData add(const SalesData sd1, const SalesData sd2) { // Not member function

	SalesData sdsum = sd1;
	sdsum.combine(sd2);
	return sdsum;
}

int main () {

	/*
	  For each of the three following declarations of combine, explain what
	  happens if we call i.combine(s), where i is a Sales_data and s is a string:
	  
	  (a) SalesData &combine(Sales_data); It does not combine as expected, i'm not sure.
	  (b) Sales_data &combine(Sales_data&);	// does not compile
	  (c) Sales_data &combine(const Sales_data&) const; // does not compile
	 */
	std::string s = "7.49";
	SalesData i("",0,0);
	i.combine(SalesData(s));
	print(std::cout, i);
	return 0;
}