Skip to content

Commit 8a9e9cd

Browse files
committed
Chapter 9 examples updated
1 parent a6a3f39 commit 8a9e9cd

Some content is hidden

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

41 files changed

+346
-401
lines changed

examples/ch09/fig09_01-02/Account.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Fig. 9.2: Account.h
2-
// Account class with a data member and
2+
// Account class with a data member and
33
// member functions to set and get its value.
44
#include <string>
55
#include <string_view>
@@ -13,7 +13,7 @@ class Account {
1313

1414
// member function that retrieves the account name from the object
1515
const std::string& getName() const {
16-
return m_name; // return the name to this function's caller
16+
return m_name; // return m_name's value to this function's caller
1717
}
1818
private:
1919
std::string m_name; // data member containing account holder's name
@@ -22,8 +22,9 @@ class Account {
2222

2323

2424

25+
2526
/**************************************************************************
26-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
27+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
2728
* Pearson Education, Inc. All Rights Reserved. *
2829
* *
2930
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_01-02/AccountTest.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
// Fig. 9.1: AccountTest.cpp
22
// Creating and manipulating an Account object.
3+
#include <fmt/format.h>
34
#include <iostream>
45
#include <string>
5-
#include <fmt/format.h> // In C++20, this will be #include <format>
66
#include "Account.h"
77

8-
using namespace std;
9-
108
int main() {
119
Account myAccount{}; // create Account object myAccount
1210

1311
// show that the initial value of myAccount's name is the empty string
14-
cout << fmt::format("Initial account name: {}\n", myAccount.getName());
12+
std::cout << fmt::format("Initial account name: {}\n",
13+
myAccount.getName());
1514

1615
// prompt for and read the name
17-
cout << "Enter the account name: ";
18-
string name{};
19-
getline(cin, name); // read a line of text
16+
std::cout << "Enter the account name: ";
17+
std::string name{};
18+
std::getline(std::cin, name); // read a line of text
2019
myAccount.setName(name); // put name in the myAccount object
2120

2221
// display the name stored in object myAccount
23-
cout << fmt::format("Updated account name: {}\n", myAccount.getName());
22+
std::cout << fmt::format("Updated account name: {}\n",
23+
myAccount.getName());
2424
}
2525

26-
27-
2826
/**************************************************************************
29-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
27+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
3028
* Pearson Education, Inc. All Rights Reserved. *
3129
* *
3230
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_03-04/Account.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ class Account {
1313

1414
// function to set the account name
1515
void setName(std::string_view name) {
16-
m_name = name; // replace m_name's value with name
16+
m_name = name;
1717
}
1818

1919
// function to retrieve the account name
2020
const std::string& getName() const {
2121
return m_name;
2222
}
2323
private:
24-
std::string m_name; // account name data member
24+
std::string m_name; // account name
2525
}; // end class Account
2626

2727

2828

2929
/**************************************************************************
30-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
30+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
3131
* Pearson Education, Inc. All Rights Reserved. *
3232
* *
3333
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_03-04/AccountTest.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
// Fig. 9.4: AccountTest.cpp
2-
// Using the Account constructor to initialize the m_name data
3-
// member at the time each Account object is created.
2+
// Using the Account constructor to initialize the m_name
3+
// data member when each Account object is created.
4+
#include <fmt/format.h>
45
#include <iostream>
5-
#include <fmt/format.h> // In C++20, this will be #include <format>
66
#include "Account.h"
77

8-
using namespace std;
9-
108
int main() {
119
// create two Account objects
1210
Account account1{"Jane Green"};
1311
Account account2{"John Blue"};
1412

1513
// display initial each Account's corresponding name
16-
cout << fmt::format("account1 name is: {}\naccount2 name is: {}\n",
17-
account1.getName(), account2.getName());
14+
std::cout << fmt::format(
15+
"account1 name is: {}\naccount2 name is: {}\n",
16+
account1.getName(), account2.getName());
1817
}
1918

2019

2120

2221
/**************************************************************************
23-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
22+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
2423
* Pearson Education, Inc. All Rights Reserved. *
2524
* *
2625
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_05-06/Account.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// Fig. 9.5: Account.h
22
// Account class with m_name and m_balance data members, and a
33
// constructor and deposit function that each perform validation.
4+
#include <algorithm>
45
#include <string>
56
#include <string_view>
67

78
class Account {
89
public:
910
// Account constructor with two parameters
1011
Account(std::string_view name, double balance)
11-
: m_name{name} { // member initializer for m_name
12-
13-
// validate that balance is greater than 0.0; if not,
14-
// data member m_balance keeps its default initial value of 0.0
15-
if (balance > 0.0) { // if the balance is valid
16-
m_balance = balance; // assign it to data member m_balance
17-
}
12+
: m_name{name}, m_balance{std::max(0.0, balance)} { // member init
13+
// empty body
1814
}
1915

2016
// function that deposits (adds) only a valid amount to the balance
@@ -40,13 +36,14 @@ class Account {
4036
}
4137
private:
4238
std::string m_name; // account name data member
43-
double m_balance{0.0}; // data member with default initial value
39+
double m_balance; // data member with default initial value
4440
}; // end class Account
4541

4642

4743

44+
4845
/**************************************************************************
49-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
46+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
5047
* Pearson Education, Inc. All Rights Reserved. *
5148
* *
5249
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_05-06/AccountTest.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
// Fig. 9.6: AccountTest.cpp
22
// Displaying and updating Account balances.
3+
#include <fmt/format.h>
34
#include <iostream>
4-
#include <fmt/format.h> // In C++20, this will be #include <format>
55
#include "Account.h"
66

7-
using namespace std;
8-
97
int main() {
108
Account account1{"Jane Green", 50.00};
119
Account account2{"John Blue", -7.00};
1210

1311
// display initial balance of each object
14-
cout << fmt::format("account1: {} balance is ${:.2f}\n",
12+
std::cout << fmt::format("account1: {} balance is ${:.2f}\n",
1513
account1.getName(), account1.getBalance());
16-
cout << fmt::format("account2: {} balance is ${:.2f}\n\n",
14+
std::cout << fmt::format("account2: {} balance is ${:.2f}\n\n",
1715
account2.getName(), account2.getBalance());
1816

19-
cout << "Enter deposit amount for account1: "; // prompt
17+
std::cout << "Enter deposit amount for account1: "; // prompt
2018
double amount;
21-
cin >> amount; // obtain user input
22-
cout << fmt::format("adding ${:.2f} to account1 balance\n\n", amount);
19+
std::cin >> amount; // obtain user input
20+
std::cout << fmt::format(
21+
"adding ${:.2f} to account1 balance\n\n", amount);
2322
account1.deposit(amount); // add to account1's balance
2423

2524
// display balances
26-
cout << fmt::format("account1: {} balance is ${:.2f}\n",
25+
std::cout << fmt::format("account1: {} balance is ${:.2f}\n",
2726
account1.getName(), account1.getBalance());
28-
cout << fmt::format("account2: {} balance is ${:.2f}\n\n",
27+
std::cout << fmt::format("account2: {} balance is ${:.2f}\n\n",
2928
account2.getName(), account2.getBalance());
3029

31-
cout << "Enter deposit amount for account2: "; // prompt
32-
cin >> amount; // obtain user input
33-
cout << fmt::format("adding ${:.2f} to account2 balance\n\n", amount);
30+
std::cout << "Enter deposit amount for account2: "; // prompt
31+
std::cin >> amount; // obtain user input
32+
std::cout << fmt::format(
33+
"adding ${:.2f} to account2 balance\n\n", amount);
3434
account2.deposit(amount); // add to account2 balance
3535

3636
// display balances
37-
cout << fmt::format("account1: {} balance is ${:.2f}\n",
37+
std::cout << fmt::format("account1: {} balance is ${:.2f}\n",
3838
account1.getName(), account1.getBalance());
39-
cout << fmt::format("account2: {} balance is ${:.2f}\n",
39+
std::cout << fmt::format("account2: {} balance is ${:.2f}\n",
4040
account2.getName(), account2.getBalance());
4141
}
4242

4343
/**************************************************************************
44-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
44+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
4545
* Pearson Education, Inc. All Rights Reserved. *
4646
* *
4747
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_07-09/Time.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
// Fig. 9.8: Time.cpp
22
// Time class member-function definitions.
3+
#include <fmt/format.h>
34
#include <stdexcept> // for invalid_argument exception class
45
#include <string>
5-
#include <fmt/format.h> // In C++20, this will be #include <format>
66
#include "Time.h" // include definition of class Time from Time.h
77

8-
using namespace std;
9-
108
// set new Time value using 24-hour time
119
void Time::setTime(int hour, int minute, int second) {
1210
// validate hour, minute and second
1311
if ((hour < 0 || hour >= 24) || (minute < 0 || minute >= 60) ||
1412
(second < 0 || second >= 60)) {
15-
throw invalid_argument{"hour, minute or second was out of range"};
13+
throw std::invalid_argument{
14+
"hour, minute or second was out of range"};
1615
}
1716

1817
m_hour = hour;
@@ -21,21 +20,22 @@ void Time::setTime(int hour, int minute, int second) {
2120
}
2221

2322
// return Time as a string in 24-hour format (HH:MM:SS)
24-
string Time::to24HourString() const {
23+
std::string Time::to24HourString() const {
2524
return fmt::format("{:02d}:{:02d}:{:02d}", m_hour, m_minute, m_second);
2625
}
2726

2827
// return Time as string in 12-hour format (HH:MM:SS AM or PM)
29-
string Time::to12HourString() const {
28+
std::string Time::to12HourString() const {
3029
return fmt::format("{}:{:02d}:{:02d} {}",
3130
((m_hour % 12 == 0) ? 12 : m_hour % 12), m_minute, m_second,
3231
(m_hour < 12 ? "AM" : "PM"));
3332
}
3433

3534

3635

36+
3737
/**************************************************************************
38-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
38+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
3939
* Pearson Education, Inc. All Rights Reserved. *
4040
* *
4141
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_07-09/Time.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Time {
1818

1919

2020
/**************************************************************************
21-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
21+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
2222
* Pearson Education, Inc. All Rights Reserved. *
2323
* *
2424
* DISCLAIMER: The authors and publisher of this book have used their *

examples/ch09/fig09_07-09/fig09_09.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// fig09_09.cpp
22
// Program to test class Time.
3-
// NOTE: This file must be compiled with Time.cpp.
3+
// NOTE: This file must be linked with Time.cpp.
4+
#include <fmt/format.h>
45
#include <iostream>
56
#include <stdexcept> // invalid_argument exception class
67
#include <string_view>
7-
#include <fmt/format.h> // In C++20, this will be #include <format>
88
#include "Time.h" // definition of class Time from Time.h
9-
using namespace std;
109

1110
// displays a Time in 24-hour and 12-hour formats
12-
void displayTime(string_view message, const Time& time) {
13-
cout << fmt::format("{}\n24-hour time: {}\n12-hour time: {}\n\n",
11+
void displayTime(std::string_view message, const Time& time) {
12+
std::cout << fmt::format("{}\n24-hour time: {}\n12-hour time: {}\n\n",
1413
message, time.to24HourString(), time.to12HourString());
1514
}
1615

@@ -25,16 +24,16 @@ int main() {
2524
try {
2625
t.setTime(99, 99, 99); // all values out of range
2726
}
28-
catch (const invalid_argument& e) {
29-
cout << fmt::format("Exception: {}\n\n", e.what());
27+
catch (const std::invalid_argument& e) {
28+
std::cout << fmt::format("Exception: {}\n\n", e.what());
3029
}
3130

3231
// display t's value after attempting to set an invalid time
3332
displayTime("After attempting to set an invalid time:", t);
3433
}
3534

3635
/**************************************************************************
37-
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
36+
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
3837
* Pearson Education, Inc. All Rights Reserved. *
3938
* *
4039
* DISCLAIMER: The authors and publisher of this book have used their *

0 commit comments

Comments
 (0)