Skip to content

Commit 6f1382b

Browse files
committed
ch7 updated
7_41 to 7_46
1 parent e3ed188 commit 6f1382b

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed

Diff for: ch7/7_41.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "7_41.h"
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
Sales_data item1;
7+
Sales_data item2("9-999-99999-2");
8+
Sales_data item3("9-999-99999-2", 3, 15);
9+
Sales_data item4(std::cin);
10+
11+
return 0;
12+
}

Diff for: ch7/7_41.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef EX7_41_H
2+
#define EX7_41_H
3+
4+
#include <string>
5+
#include <iostream>
6+
7+
using std::string;
8+
9+
class Sales_data;
10+
Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
11+
std::istream &read(std::istream &is, Sales_data &item);
12+
std::ostream &print(std::ostream &os, const Sales_data &item);
13+
14+
class Sales_data
15+
{
16+
friend Sales_data add(const Sales_data &, const Sales_data &);
17+
friend std::istream &read(std::istream &, Sales_data &);
18+
friend std::ostream &print(std::ostream &, const Sales_data &);
19+
20+
public:
21+
Sales_data() : Sales_data("", 0, 0) { std::cout << "default" << std::endl; }
22+
Sales_data(const string &s) : Sales_data(s, 0, 0) { std::cout << "only a string" << std::endl; }
23+
Sales_data(const string &s, unsigned n, double p) :
24+
bookNo(s), units_sold(n), revenue(p * n) { std::cout << "3 objects supplied" << std::endl; }
25+
Sales_data(std::istream &is) : Sales_data() { read(is, *this); std::cout << "istream" << std::endl; }
26+
27+
Sales_data &combine(const Sales_data &);
28+
string isbn() const;
29+
30+
private:
31+
inline double avg_price() const
32+
{
33+
return units_sold ? revenue / units_sold : 0;
34+
}
35+
36+
string bookNo;
37+
unsigned units_sold = 0;
38+
double revenue = 0.0;
39+
};
40+
41+
string Sales_data::isbn() const
42+
{
43+
return bookNo;
44+
}
45+
46+
Sales_data &Sales_data::combine(const Sales_data &trans)
47+
{
48+
units_sold +=trans.units_sold;
49+
revenue += trans.revenue;
50+
return *this;
51+
}
52+
53+
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
54+
{
55+
Sales_data sum = lhs;
56+
sum.combine(rhs);
57+
return sum;
58+
}
59+
60+
std::istream &read(std::istream &is, Sales_data &item)
61+
{
62+
double price = 0;
63+
is >> item.bookNo >> item.units_sold >> price;
64+
item.revenue = price * item.units_sold;
65+
return is;
66+
}
67+
68+
std::ostream &print(std::ostream &os, const Sales_data &item)
69+
{
70+
os << item.isbn() << " " << item.units_sold << " " << item.revenue;
71+
return os;
72+
}
73+
74+
#endif

Diff for: ch7/7_42.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef EX7_42_H
2+
#define EX7_42_H
3+
4+
class Book;
5+
std::istream &readBook(std::istream &, Book &);
6+
7+
class Book
8+
{
9+
public:
10+
Book() : Book("", "", "") { }
11+
Book(std::istream &is) : Book()
12+
{
13+
readBook(is, *this);
14+
}
15+
Book(std::string n, std::string::a, std::string p, std::string s) : name(n), author(a), press(p), subject(s) { }
16+
17+
private:
18+
std::string name, author, press, subject;
19+
};
20+
21+
std::istream &readBook(std::istream &is, Book &item)
22+
{
23+
is >> item.name >> item.author >> item.press >> item.subject;
24+
return is;
25+
}
26+
27+
#endif

Diff for: ch7/7_43.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef EX7_43_H
2+
#define EX7_43_H
3+
4+
class Nodefault
5+
{
6+
public:
7+
Nodefault(int i) : ival(i) { }
8+
9+
private:
10+
int ival;
11+
};
12+
13+
class C
14+
{
15+
public:
16+
C() : nd(0) { }
17+
18+
private:
19+
Nodefault nd;
20+
};
21+
22+
#endif

Diff for: ch7/7_44.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Nodefault
2+
{
3+
public:
4+
Nodefault(int i) : ival(i) { }
5+
6+
private:
7+
int ival;
8+
};
9+
10+
std::vector<NoDefault> vec(10);// illegal, have no default construction function

Diff for: ch7/7_45.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Nodefault
2+
{
3+
public:
4+
Nodefault(int i) : ival(i) { }
5+
6+
private:
7+
int ival;
8+
};
9+
10+
class C
11+
{
12+
public:
13+
C() : nd(0) { }
14+
15+
private:
16+
Nodefault nd;
17+
};
18+
19+
20+
std::vector<C> vec(10);// legal

Diff for: ch7/7_46.cc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// (a) false
2+
// (b) false
3+
// (c) false
4+
// (d) false

0 commit comments

Comments
 (0)