Skip to content

Commit 2ec0120

Browse files
committed
Improved code for lesson 6
1 parent f61b619 commit 2ec0120

File tree

14 files changed

+133
-64
lines changed

14 files changed

+133
-64
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
6+
7+
:: You can not run the program at build\main.exe
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#include <iostream>
22

3-
43
struct UserProfile {};
54
struct UserId {};
65

76
class UserProfileStorage {
8-
public:
9-
virtual UserProfile getUserProfile(const UserId& id) const = 0;
10-
11-
virtual ~UserProfileStorage() = default;
12-
13-
protected:
14-
UserProfileStorage() = default;
15-
UserProfileStorage(const UserProfileStorage&) = default;
16-
UserProfileStorage& operator=(const UserProfileStorage&) = default;
7+
public:
8+
virtual UserProfile getUserProfile(const UserId& id) const = 0;
9+
10+
virtual ~UserProfileStorage() = default;
11+
12+
protected:
13+
UserProfileStorage() = default;
14+
UserProfileStorage(const UserProfileStorage&) = default;
15+
UserProfileStorage& operator=(const UserProfileStorage&) = default;
1716
};
1817

1918
class UserProfileCache : public UserProfileStorage {
20-
public:
21-
UserProfile getUserProfile(const UserId& id) const override {
22-
std::cout << "Getting the user profile from the cache" << std::endl;
23-
return UserProfile(); }
19+
public:
20+
UserProfile getUserProfile(const UserId& id) const override {
21+
std::cout << "Getting the user profile from the cache" << std::endl;
22+
return UserProfile();
23+
}
2424
};
25+
2526
void exampleOfUsage(const UserProfileStorage& storage) {
2627
UserId user;
27-
std::cout << "About to retrieve the user profile from the storage"
28-
<<std::endl;
28+
std::cout << "About to retrieve the user profile from the storage" << std::endl;
2929
UserProfile UserProfile = storage.getUserProfile(user);
3030
}
3131

3232
int main()
3333
{
3434
UserProfileCache cache;
35-
exampleOfUsage (cache);
36-
}
35+
exampleOfUsage(cache);
36+
}

Lesson 6/Activity 26- Creating a Factory for User Profile Storage/compile.bat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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
5+
:: We copied some structures from Activity 25, so we can reuse the types defined there.
6+
:: Since we put them in an header, we need to tell the compiler to look for the headers
7+
:: in the current folder when including headers.
88
cl /EHsc /Fobuild/ /Febuild/ /W4 /I . main.cpp
99

1010
:: You can not run the program at build\main.exe

Lesson 6/Activity 26- Creating a Factory for User Profile Storage/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <iostream>
22
#include <memory>
3-
#include <userprofile_activity18.h>
3+
#include <userprofilestorage_activity25.h>
44

55
class UserProfileStorageFactory {
66
public:

Lesson 6/Activity 26- Creating a Factory for User Profile Storage/userprofile_activity18.h

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef USER_PROFILE_STORAGE_H
2+
#define USER_PROFILE_STORAGE_H
3+
4+
// Copied from Activity 25
5+
6+
struct UserProfile {};
7+
struct UserId {};
8+
9+
class UserProfileStorage {
10+
public:
11+
virtual UserProfile getUserProfile(const UserId& id) const = 0;
12+
13+
virtual ~UserProfileStorage() = default;
14+
15+
protected:
16+
UserProfileStorage() = default;
17+
UserProfileStorage(const UserProfileStorage&) = default;
18+
UserProfileStorage& operator=(const UserProfileStorage&) = default;
19+
};
20+
21+
class UserProfileCache : public UserProfileStorage {
22+
public:
23+
UserProfile getUserProfile(const UserId& id) const override {
24+
std::cout << "Getting the user profile from the cache" << std::endl;
25+
return UserProfile();
26+
}
27+
};
28+
29+
#endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
6+
7+
:: You can not run the program at build\main.exe
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
#include <future>
2+
#include <iostream>
23

34
struct DatabaseConnection {};
45

5-
void updateOrderList(DatabaseConnection&) {}
6-
void scheduleOrderProcessing(DatabaseConnection&) {}
6+
void updateOrderList(DatabaseConnection&) {
7+
std::cout << "Updating order list" << std::endl;
8+
}
9+
10+
void scheduleOrderProcessing(DatabaseConnection&) {
11+
std::cout << "Schedule order processing" << std::endl;
12+
}
13+
714
/* We need to get a copy of the shared_ptr so it stays alive until this
815
function finishes */
9-
void updateWithConnection(std::shared_ptr<DatabaseConnection>
10-
connection) {
16+
void updateWithConnection(std::shared_ptr<DatabaseConnection> connection) {
1117
updateOrderList(*connection);
1218
}
19+
1320
/* We need to get a copy of the shared_ptr so it stays alive until this
1421
function finishes. */
15-
void scheduleWithConnection(std::shared_ptr<DatabaseConnection>
16-
connection) {
22+
void scheduleWithConnection(std::shared_ptr<DatabaseConnection> connection) {
1723
scheduleOrderProcessing(*connection);
1824
}
25+
1926
int main()
2027
{
2128
std::shared_ptr<DatabaseConnection> connection = std::make_shared<DatabaseConnection>();
22-
std::async(std::launch::async, updateWithConnection, connection);
23-
std::async(std::launch::async, scheduleWithConnection, connection);
24-
}
29+
std::cout << "Updating order and scheduling order processing in parallel" << std::endl;
30+
auto updateResult = std::async(std::launch::async, updateWithConnection, connection);
31+
auto scheduleResult = std::async(std::launch::async, scheduleWithConnection, connection);
32+
33+
updateResult.wait();
34+
scheduleResult.wait();
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
6+
7+
:: You can not run the program at build\main.exe
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include <iostream>
2+
23
// first base class
34
class Vehicle {
45
public:
56
int getTankCapacity(){
67
const int tankLiters = 10;
7-
std::cout << "The current tank capacity for your car is " <<
8-
tankLiters << " Liters."<<std::endl;
9-
return tankLiters;
8+
std::cout << "The current tank capacity for your car is " << tankLiters << " Liters."<<std::endl;
9+
return tankLiters;
1010
}
1111
};
1212

@@ -21,18 +21,17 @@ class CollectorItem {
2121
// Subclass derived from two base classes
2222
class Ferrari250GT: protected Vehicle, public CollectorItem {
2323
public:
24-
Ferrari250GT() {
25-
std::cout << "Thank you for buying the Ferrari 250 GT with tank capacity " << getTankCapacity() << std::endl;
26-
//return 0;
27-
}
24+
Ferrari250GT() {
25+
std::cout << "Thank you for buying the Ferrari 250 GT with tank capacity " << getTankCapacity() << std::endl;
26+
}
2827
};
2928

3029
int main()
3130
{
3231
Ferrari250GT ferrari;
3332
std::cout << "The value of the Ferrari is " << ferrari.getValue() <<
3433
std::endl;
35-
34+
3635
/* Cannot call ferrari.getTankCapacity() because Ferrari250GT inherits from Vehicle with the protected specifier */
37-
return 0;
36+
return 0;
3837
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 6/Exercise 21 Using Multiple Inheritance to Create a Welcome to the/main.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
#include <iostream>
22

33
class DataScienceDev {
4-
public:
5-
DataScienceDev(){
6-
std::cout << "Welcome to the Data Science Developer Community."
7-
<< std::endl;
8-
}
4+
public:
5+
DataScienceDev(){
6+
std::cout << "Welcome to the Data Science Developer Community." << std::endl;
7+
}
98
};
109

1110
class FutureCppDev {
12-
public:
13-
FutureCppDev(){
14-
std::cout << "Welcome to the C++ Developer Community." <<
15-
std::endl;
16-
}
11+
public:
12+
FutureCppDev(){
13+
std::cout << "Welcome to the C++ Developer Community." << std::endl;
14+
}
1715
};
1816

1917
class Student : public DataScienceDev, public FutureCppDev {
20-
public:
18+
public:
2119
Student(){
22-
std::cout << "Student is a Data Developer and C++ Developer."
23-
<< std::endl;
20+
std::cout << "Student is a Data Developer and C++ Developer." << std::endl;
2421
}
2522
};
2623

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ /W4 main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 6/Exercise 22 Exploring the Virtual Method/main.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
#include <iostream>
22

33
class Vehicle {
4-
public:
5-
void turnOn(){
6-
std::cout<< "Vehicle: turn on" << std::endl;
7-
}
4+
public:
5+
virtual void turnOn(){
6+
std::cout<< "Vehicle: turn on" << std::endl;
7+
}
88
};
99

1010
class Car : public Vehicle {
11-
public:
12-
virtual void turnOn() {
13-
std::cout << "Car: turn on" << std::endl;
14-
}
11+
public:
12+
virtual void turnOn() {
13+
std::cout << "Car: turn on" << std::endl;
14+
}
1515
};
1616

1717
void myTurnOn(Vehicle& vehicle) {
18-
std::cout << "Calling turnOn() on the vehicle reference" <<
19-
std::endl;
18+
std::cout << "Calling turnOn() on the vehicle reference" << std::endl;
2019
vehicle.turnOn();
2120
}
2221

0 commit comments

Comments
 (0)