Skip to content

Commit f61b619

Browse files
authored
Merge pull request TrainingByPackt#8 from TrainingByPackt/chapter5
Added compilation script and fixed exercises
2 parents 2395b48 + ed8e30d commit f61b619

File tree

22 files changed

+209
-144
lines changed

22 files changed

+209
-144
lines changed

Lesson 5/Activity 19- Storing User Accounts/compile.bat

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@
22
mkdir build
33

44
:: Build main.cpp
5-
:: We include the header from Activity 18, so we can reuse the types defined there
6-
:: To do it, we created a symbolic link in the current folder to the header in Activity 18
7-
:: so that it's easy to access the file while working from the folder for Activity 19
8-
cl /EHsc /Fobuild/ /Febuild/ /W4 /I . main.cpp
9-
10-
:: You can not run the program at build\main.exe
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
#include <iostream>
2-
#include <memory>
3-
#include <userprofile_activity18.h>
2+
#include <array>
43

5-
class UserProfileStorageFactory {
6-
public:
7-
// Note: we are creating the derived class in the body of the function
8-
std::unique_ptr<UserProfileStorage> create() const {
9-
return std::make_unique<UserProfileCache>();
4+
int main()
5+
{
6+
std::array<int, 10> accounts;
7+
for(std::size_t i = 0; i < accounts.size(); i++) {
8+
accounts[i] = i;
109
}
11-
};
12-
13-
14-
void getUserProfile(const UserProfileStorageFactory& storageFactory) {
15-
// The current function "owns" the memory for the UserProfileStorage
16-
// though the unique_ptr, but it doesn't know which instance of the
17-
// storage it is using. It could even change during run time.
18-
std::unique_ptr<UserProfileStorage> storage = storageFactory.create();
19-
UserId user;
20-
std::cout << "Retrieving user profile from the factory generated storage" << std::endl;
21-
storage->getUserProfile(user);
22-
// The storage is automatically destroyed
23-
}
2410

11+
accounts.front() = 100;
12+
accounts.back() = 200;
2513

26-
int main()
27-
{
28-
UserProfileStorageFactory factory;
29-
getUserProfile(factory);
14+
std::cout << "Elements:";
15+
for(int& account: accounts) {
16+
std::cout << " " << account;
17+
}
18+
std::cout << std::endl;
3019
}

Lesson 5/Activity 19- Storing User Accounts/userprofile_activity18.h

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp

Lesson 5/Activity 20-Retrieving a User's Balance from their Given Username/main.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ int main()
1010
balances.insert(std::make_pair("Alice",50));
1111
balances.insert(std::make_pair("Bob", 50));
1212
balances.insert(std::make_pair("Charlie", 50));
13-
13+
1414
auto donaldAccountPos = balances.find("Donald");
1515
bool hasAccount = (donaldAccountPos != balances.end());
16-
std::cout << "Donald has an account: " << hasAccount << std::endl;
17-
16+
std::cout << "Donald has an account: " << std::boolalpha << hasAccount << std::endl;
17+
1818
auto alicePosition = balances.find("Alice");
19-
std::cout << "Alice balance is: " << alicePosition->second <<
20-
std::endl;
21-
19+
std::cout << "Alice balance is: " << alicePosition->second << std::endl;
2220
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
#include <stack>
2+
#include <string>
3+
#include <iostream>
24

3-
std::stack<RegistrationForm> registrationForms;
4-
stack.push(form);
5-
std::cout << “Pushed form for user “ << form.userName << std::endl;
6-
while(not stack.empty()) {
7-
processRegistration(stack.top());
8-
stack.pop();
5+
struct RegistrationForm {
6+
std::string userName;
7+
};
8+
9+
void processRegistration(RegistrationForm form) {
10+
std::cout << "Processing form for user: " << form.userName << std::endl;
11+
}
12+
13+
void storeRegistrationForm(std::stack<RegistrationForm>& stack, RegistrationForm form) {
14+
// Add the form to the top of the stack
15+
stack.push(form);
16+
std::cout << "Pushed form for user " << form.userName << std::endl;
17+
}
18+
19+
void endOfDayRegistrationProcessing(std::stack<RegistrationForm>& stack) {
20+
// Process the top of the stack and then remove it
21+
while ( !stack.empty()) {
22+
processRegistration(stack.top());
23+
stack.pop();
24+
}
925
}
1026

1127
int main(){
1228
std::stack<RegistrationForm> registrationForms;
13-
storeRegistrationForm(registrationForms, RegistrationForm{“Alice”});
14-
storeRegistrationForm(registrationForms, RegistrationForm{“Bob”});
15-
storeRegistrationForm(registrationForms,
16-
RegistrationForm{“Charlie”});
29+
30+
storeRegistrationForm(registrationForms, RegistrationForm{"Alice"});
31+
storeRegistrationForm(registrationForms, RegistrationForm{"Bob"});
32+
storeRegistrationForm(registrationForms, RegistrationForm{"Charlie"});
33+
1734
endOfDayRegistrationProcessing(registrationForms);
1835
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 /std:c++17 main.cpp

Lesson 5/Activity 22 Airport System Management/main.cpp

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,75 @@
33
#include <variant>
44

55
struct AtGate {
6-
int gate;
6+
int gate;
77
};
8+
89
struct Taxi {
9-
int lane;
10-
int numPassengers;
10+
int lane;
11+
int numPassengers;
1112
};
13+
1214
struct Flying {
13-
float speed;
15+
float speed;
1416
};
1517

1618
class Airplane {
17-
std::variant<AtGate, Taxi, Flying> state;
18-
public:
19-
Airplane(int gate) : state(AtGate{gate}) {
20-
std::cout << "At gate " << gate << std::endl;
21-
}
19+
std::variant<AtGate, Taxi, Flying> state;
20+
public:
21+
Airplane(int gate) : state(AtGate{gate}) {
22+
std::cout << "At gate " << gate << std::endl;
23+
}
2224

23-
void startTaxi(int lane, int numPassengers) {
24-
if (not std::holds_alternative<AtGate>(state)) {
25-
std::cout << "Not at gate: the plane cannot start taxi to lane " << lane << std::endl;
26-
return;
27-
}
28-
std::cout << "Taxing to lane " << lane << std::endl;
29-
state = Taxi{lane, numPassengers};
30-
}
25+
void startTaxi(int lane, int numPassengers) {
26+
if (!std::holds_alternative<AtGate>(state)) {
27+
std::cout << "Not at gate: the plane cannot start taxi to lane " << lane << std::endl;
28+
return;
29+
}
30+
std::cout << "Taxing to lane " << lane << std::endl;
31+
state = Taxi{lane, numPassengers};
32+
}
3133

32-
void takeOff( float speed) {
33-
if (not std::holds_alternative<Taxi>(state)) {
34-
std::cout << "Not at lane: the plane cannot take off with speed " << speed << std::endl;
35-
return;
36-
}
37-
std::cout << "Taking off at speed " << speed << std::endl;
38-
state = Flying{speed};
39-
}
34+
void takeOff( float speed) {
35+
if (!std::holds_alternative<Taxi>(state)) {
36+
std::cout << "Not at lane: the plane cannot take off with speed " << speed << std::endl;
37+
return;
38+
}
39+
std::cout << "Taking off at speed " << speed << std::endl;
40+
state = Flying{speed};
41+
}
4042

41-
void currentStatus() {
42-
AirplaneStateVisitor visitor;
43-
std::visit(visitor, state);
44-
}
43+
void currentStatus() {
44+
AirplaneStateVisitor visitor;
45+
std::visit(visitor, state);
46+
}
4547

46-
class AirplaneStateVisitor {
47-
public:
48-
void operator()(const AtGate& atGate) {
49-
std::cout <<"AtGate: "<< atGate.gate << std::endl;
50-
}
51-
void operator()(const Taxi& taxi) {
52-
std::cout <<"Taxi: lane "<< taxi.lane << " with " << taxi.
53-
numPassengers << " passengers" << std::endl;
54-
}
55-
void operator()(const Flying& flying) {
56-
std::cout <<"Flying: speed "<< flying.speed << std::endl;
57-
}
58-
};
48+
class AirplaneStateVisitor {
49+
public:
50+
void operator()(const AtGate& atGate) {
51+
std::cout <<"AtGate: "<< atGate.gate << std::endl;
52+
}
53+
54+
void operator()(const Taxi& taxi) {
55+
std::cout <<"Taxi: lane "<< taxi.lane << " with " << taxi.
56+
numPassengers << " passengers" << std::endl;
57+
}
58+
59+
void operator()(const Flying& flying) {
60+
std::cout <<"Flying: speed "<< flying.speed << std::endl;
61+
}
62+
};
5963
};
6064

6165

6266
int main()
6367
{
64-
Airplane airplane(52);
65-
airplane.currentStatus();
66-
airplane.startTaxi(12, 250);
67-
airplane.currentStatus();
68-
airplane.startTaxi(13, 250);
69-
airplane.currentStatus();
70-
airplane.takeOff(800);
71-
airplane.currentStatus();
72-
airplane.takeOff(900);
73-
}
68+
Airplane airplane(52);
69+
airplane.currentStatus();
70+
airplane.startTaxi(12, 250);
71+
airplane.currentStatus();
72+
airplane.startTaxi(13, 250);
73+
airplane.currentStatus();
74+
airplane.takeOff(800);
75+
airplane.currentStatus();
76+
airplane.takeOff(900);
77+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 /std:c++17 main.cpp

Lesson 5/Exercise 13- Using Variant in the program/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
int main()
66
{
77
std::variant<std::string, int> variant = 42;
8-
std::cout << get <1>(variant) << std::endl;
9-
std::cout << get <int>(variant) << std::endl;
10-
}
8+
std::cout << std::get<1>(variant) << std::endl;
9+
std::cout << std::get<int>(variant) << std::endl;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 /std:c++17 main.cpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#include <iostream>
22
#include <vector>
3+
34
int main()
45
{
5-
std::vector<int> numbers = {1, 2, 3, 4, 5};
6-
auto it = numbers.begin();
7-
std::cout << *it << std::endl; // dereference: points to 1
8-
it++; // increment: now it points to 2
9-
std::cout << *it << std::endl;
10-
// random access: access the 2th element after the current one
11-
std::cout << it[2] << std::endl;
12-
--it; // decrement: now it points to 1 again
13-
std::cout << *it << std::endl;
14-
it += 4; // advance the iterator by 4 positions: now it points to 5
15-
std::cout << *it << std::endl;
16-
it++; // advance past the last element;
17-
std::cout << "'it' is after the past element: " << (it == numbers.
18-
end()) << std::endl;
6+
std::vector<int> numbers = {1, 2, 3, 4, 5};
7+
auto it = numbers.begin();
8+
std::cout << *it << std::endl; // dereference: points to 1
9+
it++; // increment: now it points to 2
10+
std::cout << *it << std::endl;
11+
// random access: access the 2th element after the current one
12+
std::cout << it[2] << std::endl;
13+
--it; // decrement: now it points to 1 again
14+
std::cout << *it << std::endl;
15+
it += 4; // advance the iterator by 4 positions: now it points to 5
16+
std::cout << *it << std::endl;
17+
it++; // advance past the last element;
18+
std::cout << "'it' is after the last element: " <<
19+
std::boolalpha << (it == numbers.end()) << std::endl;
1920
}
20-
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp

Lesson 5/Exercise 17 Stream Iterator/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ int main()
88
std::istream_iterator<int> it = std::istream_iterator<int>(std::cin);
99
std::istream_iterator<int> end;
1010
for(; it != end; ++it) {
11-
std::cout << "The number is: " << *it << std::endl;
11+
std::cout << "The number is: " << *it << std::endl;
1212
}
13-
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include <iostream>
22
#include <string>
33
#include <vector>
4-
#include <list>
54

6-
using namespace std;
75
int main(){
8-
std::vector<int> numbers = {10, 34, 64, 97, 56, 43, 50, 89, 32, 5};
6+
std::vector<int> numbers = {10, 34, 64, 97, 56, 43, 50, 89, 32, 5};
97

10-
for (auto pos = numbers.begin(); pos != numbers.end(); ++pos){
11-
std::cout << "Balance: " << *pos << std::endl;
12-
}
8+
for (auto pos = numbers.begin(); pos != numbers.end(); ++pos){
9+
std::cout << "Balance: " << *pos << std::endl;
10+
}
1311
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp

0 commit comments

Comments
 (0)