Skip to content

Commit c5e1268

Browse files
committed
Generated on 2022-10-06
1 parent 96ca86a commit c5e1268

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1066
-1054
lines changed

headers/04-scope.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ using namespace std;
66
int alice_height_m;
77

88
void victorian_england() {
9-
size_t alice_height_m{1};
9+
size_t alice_height_m{ 1 };
1010
cout << "In \"victorian_england()\", alice_height_m is " << alice_height_m << ".\n";
1111
}
1212

1313
void wonderland() {
14-
double alice_height_m{0.15};
14+
double alice_height_m{ 0.15 };
1515
cout << "In \"wonderland()\", alice_height_m is " << alice_height_m << ".\n";
1616
}
1717

headers/05-pointer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ using namespace std;
55

66
int main() {
77
int i{ -1 }, j{};
8-
int *p;
9-
p = &i; // set p to address of i
8+
int *p; // define p as an int*
9+
p = &i; // set p to address of i
1010
cout << "(1) p = " << p << ", *p = " << *p << ", i = " << i << '\n';
1111
cout << "Please enter an integer: ";
1212
cin >> j;
13-
*p = j; // assign the value of j to the variable p points to
13+
*p = j; // assign the value of j to the variable p points to
1414
cout << "(2) p = " << p << ", *p = " << *p << ", i = " << i << '\n';
1515
}

headers/06-point1.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44
using namespace std;
55

66
struct Point {
7-
void setX(int nx) {
8-
x = nx;
9-
}
10-
void setY(int ny) {
11-
y = ny;
12-
}
13-
auto getXY() const {
14-
return pair{x, y};
15-
}
7+
void setX(int nx) {
8+
x = nx;
9+
}
10+
void setY(int ny) {
11+
y = ny;
12+
}
13+
auto getXY() const {
14+
return pair{x, y};
15+
}
1616
private:
17-
int x{}, y{};
17+
int x{}, y{};
1818
};
1919

2020
int main() {
21-
Point p;
22-
int user_x{}, user_y{};
23-
cout << "Please enter x and y for Point:\n";
24-
cin >> user_x >> user_y;
25-
p.setX(user_x);
26-
p.setY(user_y);
27-
auto [ px, py ] = p.getXY();
28-
cout << "px = " << px << ", py = " << py << '\n';
21+
Point p;
22+
int user_x{}, user_y{};
23+
cout << "Please enter x and y for Point:\n";
24+
cin >> user_x >> user_y;
25+
p.setX(user_x);
26+
p.setY(user_y);
27+
auto [ px, py ] = p.getXY();
28+
cout << "px = " << px << ", py = " << py << '\n';
2929
}

headers/06-point2.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,46 @@
44
using namespace std;
55

66
struct Point {
7-
void setX(int nx)
8-
{
9-
if (nx < 0) {
10-
x = 0;
11-
}
12-
else if (nx > screenX) {
13-
x = screenX;
14-
}
15-
else {
16-
x = nx;
17-
}
18-
}
19-
void setY(int ny)
20-
{
21-
if (ny < 0) {
22-
y = 0;
23-
}
24-
else if (ny > screenY) {
25-
y = screenY;
26-
}
27-
else {
28-
y = ny;
29-
}
30-
}
31-
auto getXY() const {
32-
return pair{x, y};
7+
void setX(int nx)
8+
{
9+
if (nx < 0) {
10+
x = 0;
11+
}
12+
else if (nx > screenX) {
13+
x = screenX;
14+
}
15+
else {
16+
x = nx;
17+
}
3318
}
34-
static const int screenX{ 639 }, screenY{ 479 };
19+
void setY(int ny)
20+
{
21+
if (ny < 0) {
22+
y = 0;
23+
}
24+
else if (ny > screenY) {
25+
y = screenY;
26+
}
27+
else {
28+
y = ny;
29+
}
30+
}
31+
auto getXY() const {
32+
return pair{x, y};
33+
}
34+
static const int screenX{ 639 }, screenY{ 479 };
3535
private:
36-
int x{}, y{};
36+
int x{}, y{};
3737
};
3838

3939
int main() {
40-
cout << "Screen is " << Point::screenX + 1 << " by " << Point::screenY + 1 << '\n';
41-
Point p;
42-
int user_x{}, user_y{};
43-
cout << "Please enter x and y for Point:\n";
44-
cin >> user_x >> user_y;
45-
p.setX(user_x);
46-
p.setY(user_y);
47-
auto [ px, py ] = p.getXY();
48-
cout << "px = " << px << ", py = " << py << '\n';
40+
cout << "Screen is " << Point::screenX + 1 << " by " << Point::screenY + 1 << '\n';
41+
Point p;
42+
int user_x{}, user_y{};
43+
cout << "Please enter x and y for Point:\n";
44+
cin >> user_x >> user_y;
45+
p.setX(user_x);
46+
p.setY(user_y);
47+
auto [ px, py ] = p.getXY();
48+
cout << "px = " << px << ", py = " << py << '\n';
4949
}

headers/06-point3.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
using namespace std;
55

66
struct Point{
7-
int x{}, y{};
7+
int x{}, y{};
88
};
99

1010
Point operator+ (const Point& lhs, const Point& rhs) {
11-
Point result;
12-
result.x = lhs.x + rhs.x;
13-
result.y = lhs.y + rhs.y;
14-
return result;
11+
Point result;
12+
result.x = lhs.x + rhs.x;
13+
result.y = lhs.y + rhs.y;
14+
return result;
1515
}
1616

1717
int main() {
18-
Point p1{ 100, 200 }, p2{ 200, -50 }, p3;
19-
p3 = p1 + p2;
20-
cout << "p3 = (" << p3.x << ',' << p3.y << ")\n";
18+
Point p1{ 100, 200 }, p2{ 200, -50 }, p3;
19+
p3 = p1 + p2; // use overloaded "operator+"
20+
cout << "p3 = (" << p3.x << ',' << p3.y << ")\n";
2121
}

headers/06-point4.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
using namespace std;
55

66
struct Point{
7-
int x{}, y{};
7+
int x{}, y{};
88

9-
Point& operator+= (const Point& rhs)
10-
{
11-
x += rhs.x;
12-
y += rhs.y;
13-
return *this;
14-
}
9+
Point& operator+= (const Point& rhs) { // member operator +=
10+
x += rhs.x;
11+
y += rhs.y;
12+
return *this;
13+
}
1514
};
1615

17-
Point operator+ (const Point& lhs, const Point& rhs) {
18-
Point result{ lhs };
19-
result += rhs;
20-
return result;
16+
Point operator+ (const Point& lhs, const Point& rhs) { // non-member operator+
17+
Point result{ lhs };
18+
result += rhs;
19+
return result;
2120
}
2221

2322
int main() {
24-
Point p1{ 100, 200 }, p2{ 200, -50 }, p3;
25-
p3 = p1 + p2;
26-
cout << "p3 = (" << p3.x << ',' << p3.y << ")\n";
23+
Point p1{ 100, 200 }, p2{ 200, -50 }, p3;
24+
p3 = p1 + p2;
25+
cout << "p3 = (" << p3.x << ',' << p3.y << ")\n";
2726
}

headers/07-lists.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main() {
2020
++iter; // note: must "keep up"
2121
}
2222

23-
list<string> lst(begin(fwd), end(fwd));
23+
list<string> lst(begin(fwd), end(fwd)); // copy fwd into lst
2424
lst.sort();
2525
for (const auto& e : lst) {
2626
cout << "- " << e << '\n';

headers/07-span.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void print_ints(span<int> s) {
1818
int main() {
1919
int c_array[] = { 1, 2, 3 };
2020
vector vec = { 2, 6, 4, 3 };
21-
array<int,4> std_array = { 3, 2, 1 };
21+
array<int,4> std_array = { 7, 6, 5 };
2222

2323
print_ints(c_array);
2424
print_ints(vec);

headers/07-string-upper.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
using namespace std;
77

88
void string_to_uppercase(string &s) {
9-
for (auto& c : s) {
10-
c = toupper(c);
11-
}
9+
for (auto& c : s) {
10+
c = toupper(c);
11+
}
1212
}
1313

1414
int main() {
15-
cout << "Please enter some text in lower-, mixed- or upper-case:\n";
16-
string input;
17-
getline(cin, input);
18-
string_to_uppercase(input);
19-
cout << "The same text in uppercase is:\n" << input << '\n';
15+
cout << "Please enter some text in lower-, mixed- or upper-case:\n";
16+
string input;
17+
getline(cin, input);
18+
string_to_uppercase(input);
19+
cout << "The same text in uppercase is:\n" << input << '\n';
2020
}

headers/08-file1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) {
1212
ifstream infile{ argv[1] };
1313

1414
int c = infile.get();
15-
while (c != EOF) {
15+
while (c != ifstream::traits_type::eof()) {
1616
cout << static_cast<char>(c);
1717
c = infile.get();
1818
}

0 commit comments

Comments
 (0)