-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly.cpp
More file actions
88 lines (80 loc) · 2.17 KB
/
poly.cpp
File metadata and controls
88 lines (80 loc) · 2.17 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cstdio>
#include <memory>
#include <expected>
class Animal
{
private:
virtual void speak (void) = 0;
virtual void legs (void) = 0;
public:
std::expected<void, std::string> trigger_proxy(std::unique_ptr<Animal> &ptr);
virtual ~Animal(void)
{
std::printf("Animal destroyed!!\n");
}
};
class Human : public Animal
{
public:
void speak(void) override;
void legs(void) override;
~Human(void)
{
std::printf("Human destroyed!!\n");
}
};
class Duck : public Animal
{
public:
void speak(void) override;
void legs(void) override;
~Duck(void)
{
std::printf("Duck destroyed!!\n");
}
};
void Human::speak(void)
{
std::printf("Human speak speak!!\n");
}
void Human::legs(void)
{
std::printf("Humans are bipedal...\n");
}
void Duck::speak(void)
{
std::printf("Duck speak speak!!\n");
}
void Duck::legs(void)
{
std::printf("Ducks are bipedal...\n");
}
std::expected<void, std::string> Animal::trigger_proxy(std::unique_ptr<Animal> &ptr)
{
if(ptr == nullptr)
return std::unexpected("trigger_proxy called when unique_ptr ptr is null");
ptr->speak();
ptr->legs();
return {};
}
int main(void)
{
std::unique_ptr<Animal> ptr = nullptr;
ptr = std::make_unique<Human>();
auto num = ptr->trigger_proxy(ptr);
if(!num.has_value())
std::printf("\nError msg: %s", num.error().c_str());
std::printf("\n");
ptr = nullptr;
std::printf("\n");
ptr = std::make_unique<Duck>();
num = ptr->trigger_proxy(ptr);
if(!num.has_value())
std::printf("\nError msg: %s", num.error().c_str());
std::printf("\n");
ptr = nullptr;
num = ptr->trigger_proxy(ptr);
if(!num.has_value())
std::printf("\nError msg: %s", num.error().c_str());
return 0;
}