Skip to content

Commit d3c7ccd

Browse files
committed
added progress
1 parent 0df1db0 commit d3c7ccd

26 files changed

+17839
-0
lines changed

Chapter18/18.12/chapter01.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#ifndef CHAPTER01_H
2+
#define CHAPTER01_H
3+
4+
/*
5+
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
6+
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
7+
* copyright and warranty notices given in that book:
8+
*
9+
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
10+
*
11+
*
12+
* "The authors and publisher have taken care in the preparation of this book,
13+
* but make no expressed or implied warranty of any kind and assume no
14+
* responsibility for errors or omissions. No liability is assumed for
15+
* incidental or consequential damages in connection with or arising out of the
16+
* use of the information or programs contained herein."
17+
*
18+
* Permission is granted for this code to be used for educational purposes in
19+
* association with the book, given proper citation if and when posted or
20+
* reproduced.Any commercial use of this code requires the explicit written
21+
* permission of the publisher, Addison-Wesley Professional, a division of
22+
* Pearson Education, Inc. Send your request for permission, stating clearly
23+
* what code you would like to use, and in what specific way, to the following
24+
* address:
25+
*
26+
* Pearson Education, Inc.
27+
* Rights and Permissions Department
28+
* One Lake Street
29+
* Upper Saddle River, NJ 07458
30+
* Fax: (201) 236-3290
31+
*/
32+
33+
/* This file defines the Sales_item class used in chapter 1.
34+
* The code used in this file will be explained in
35+
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
36+
* Readers shouldn't try to understand the code in this file
37+
* until they have read those chapters.
38+
*/
39+
40+
// Definition of Sales_item class and related functions goes here
41+
#include <iostream>
42+
#include <string>
43+
44+
namespace cplusplus_primer {
45+
namespace chapter01 {
46+
class Sales_item {
47+
// these declarations are explained section 7.2.1, p. 270
48+
// and in chapter 14, pages 557, 558, 561
49+
friend std::istream& operator>>(std::istream&, Sales_item&);
50+
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
51+
friend bool operator<(const Sales_item&, const Sales_item&);
52+
friend bool
53+
operator==(const Sales_item&, const Sales_item&);
54+
public:
55+
// constructors are explained in section 7.1.4, pages 262 - 265
56+
// default constructor needed to initialize members of built-in type
57+
Sales_item(): units_sold(0), revenue(0.0) { }
58+
Sales_item(const std::string &book):
59+
bookNo(book), units_sold(0), revenue(0.0) { }
60+
Sales_item(std::istream &is) { is >> *this; }
61+
public:
62+
// operations on Sales_item objects
63+
// member binary operator: left-hand operand bound to implicit this pointer
64+
Sales_item& operator+=(const Sales_item&);
65+
66+
// operations on Sales_item objects
67+
std::string isbn() const { return bookNo; }
68+
double avg_price() const;
69+
// private members as before
70+
private:
71+
std::string bookNo; // implicitly initialized to the empty string
72+
unsigned units_sold;
73+
double revenue;
74+
};
75+
76+
// used in chapter 10
77+
inline
78+
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
79+
{ return lhs.isbn() == rhs.isbn(); }
80+
81+
// nonmember binary operator: must declare a parameter for each operand
82+
Sales_item operator+(const Sales_item&, const Sales_item&);
83+
84+
inline bool
85+
operator==(const Sales_item &lhs, const Sales_item &rhs)
86+
{
87+
// must be made a friend of Sales_item
88+
return lhs.units_sold == rhs.units_sold &&
89+
lhs.revenue == rhs.revenue &&
90+
lhs.isbn() == rhs.isbn();
91+
}
92+
93+
inline bool
94+
operator!=(const Sales_item &lhs, const Sales_item &rhs)
95+
{
96+
return !(lhs == rhs); // != defined in terms of operator==
97+
}
98+
99+
// assumes that both objects refer to the same ISBN
100+
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
101+
{
102+
units_sold += rhs.units_sold;
103+
revenue += rhs.revenue;
104+
return *this;
105+
}
106+
107+
// assumes that both objects refer to the same ISBN
108+
Sales_item
109+
operator+(const Sales_item& lhs, const Sales_item& rhs)
110+
{
111+
Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
112+
ret += rhs; // add in the contents of (|rhs|)
113+
return ret; // return (|ret|) by value
114+
}
115+
116+
std::istream&
117+
operator>>(std::istream& in, Sales_item& s)
118+
{
119+
double price;
120+
in >> s.bookNo >> s.units_sold >> price;
121+
// check that the inputs succeeded
122+
if (in)
123+
s.revenue = s.units_sold * price;
124+
else
125+
s = Sales_item(); // input failed: reset object to default state
126+
return in;
127+
}
128+
129+
std::ostream&
130+
operator<<(std::ostream& out, const Sales_item& s)
131+
{
132+
out << s.isbn() << " " << s.units_sold << " "
133+
<< s.revenue << " " << s.avg_price();
134+
return out;
135+
}
136+
137+
double Sales_item::avg_price() const
138+
{
139+
if (units_sold)
140+
return revenue/units_sold;
141+
else
142+
return 0;
143+
}
144+
} // end chapter01 namespace
145+
} // end cplusplus_primer namespace
146+
#endif

Chapter18/18.12/chapter02.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef CHAPTER02_H
2+
#define CHAPTER02_H
3+
4+
#include <string>
5+
6+
namespace cplusplus_primer {
7+
namespace chapter02 {
8+
struct Sales_data {
9+
std::string bookNo;
10+
unsigned units_sold = 0;
11+
double revenue = 0.0;
12+
};
13+
}
14+
}
15+
16+
#endif

Chapter18/18.12/chapter03.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef CHAPTER03_H
2+
#define CHAPTER03_H
3+
4+
#include <string>
5+
6+
namespace cplusplus_primer {
7+
namespace chapter03 {
8+
struct Sales_data {
9+
std::string bookNo;
10+
unsigned units_sold = 0;
11+
double revenue = 0.0;
12+
};
13+
}
14+
}
15+
16+
#endif

Chapter18/18.12/chapter06.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef CHAPTER06_H
2+
#define CHAPTER06_H
3+
4+
#include <string>
5+
6+
namespace cplusplus_primer {
7+
namespace chapter06 {
8+
inline bool isShorter(const std::string &s1, const std::string &s2) {
9+
return s1.size() < s2.size();
10+
}
11+
}
12+
}
13+
14+
#endif

Chapter18/18.12/chapter07.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "chapter07.h"
2+
3+
namespace cplusplus_primer {
4+
namespace chapter07 {
5+
double Account::initRate() {
6+
return 0.25;
7+
}
8+
9+
void Account::rate(double newRate) {
10+
interestRate = newRate;
11+
}
12+
13+
double Account::interestRate = initRate();
14+
15+
constexpr int Account::period;
16+
17+
Sales_data &Sales_data::combine(const Sales_data &rhs) {
18+
units_sold += rhs.units_sold;
19+
revenue += rhs.revenue;
20+
return *this;
21+
}
22+
23+
// NONMEMBER FUNCTIONS
24+
25+
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
26+
Sales_data sum = lhs;
27+
sum.combine(rhs);
28+
return sum;
29+
}
30+
31+
std::istream &read(std::istream &is, Sales_data &item) {
32+
double price = 0.0;
33+
is >> item.bookNo >> item.units_sold >> price;
34+
item.revenue = item.units_sold * price;
35+
return is;
36+
}
37+
38+
std::ostream &print(std::ostream &os, const Sales_data &item) {
39+
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
40+
return os;
41+
}
42+
43+
Window_mgr::Window_mgr() : screens{Screen(24, 80, ' ')} {}
44+
45+
void Window_mgr::clear(ScreenIndex i) {
46+
Screen &s = screens[i];
47+
s.contents = std::string(s.height * s.width, ' ');
48+
}
49+
50+
void Screen::some_member() const {
51+
++access_ctr;
52+
}
53+
54+
std::istream &read(std::istream &is, Person &p) {
55+
is >> p.name >> p.address;
56+
return is;
57+
}
58+
59+
std::ostream &print(std::ostream &os, const Person &p) {
60+
os << p.getName() << " " << p.getAddress();
61+
return os;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)