-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_solution_11.cpp
70 lines (63 loc) · 1.56 KB
/
Lab_solution_11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <exception>
#include <memory>
using namespace std;
class Device
{
public:
Device(std::string const model, int systemID, bool isConnected = false)
{
cout << "Construct Device Object "<< model << endl;
m_model = model;
m_systemID = systemID;
m_isConnected = isConnected;
}
~Device()
{
cout << "Destroy Device Object "<< m_model << endl;
}
protected:
std::string m_model;
private:
int m_systemID = 0;
bool m_isConnected = false;
};
int createAnotherObject()
{
Device myDevice3("Device 3", 0x245);
cout << "Throw exception : logic_error" << endl;
throw logic_error("Illogic");
return 0;
}
int createObject()
{
Device myDevice2("Device 2", 0x256);
int ret = 0;
try
{
ret = createAnotherObject();
cout << "After calling createAnotherObject()" << endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
cout << "After try/catch block createObject()" << endl;
return ret;
}
int main()
{
try
{
Device myDevice1("Device 1", 0x264);
int ret = createObject();
cout << "createObject() returned " << ret << endl;
}
catch (exception& e)
{
cout << "In catch: exception: " << e.what() << endl;
}
cout << "After try/catch block main()" << endl;
cout << "End of main" << endl;
return 0;
}