-
Notifications
You must be signed in to change notification settings - Fork 0
/
penz.cpp
116 lines (95 loc) · 2.89 KB
/
penz.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>
using namespace std;
class Penz {
protected:
unsigned ertek;
public:
Penz(unsigned e) : ertek(e) {}
virtual ~Penz() = default;
unsigned getErtek() const { return ertek; }
virtual string toString() const = 0;
};
class Erme : public Penz {
string anyag;
unsigned atmero;
public:
Erme(unsigned e, const string& a, unsigned at) : Penz(e), anyag(a), atmero(at) {}
string toString() const override {
return anyag + ":" + to_string(atmero);
}
};
class Bankjegy : public Penz {
string abra;
public:
Bankjegy(unsigned e, const string& a) : Penz(e), abra(a) {}
string toString() const override {
return abra;
}
};
class Penztarca {
vector<unique_ptr<Penz>> penzek;
public:
Penztarca() = default;
Penztarca(const Penztarca& other) {
for (const auto& p : other.penzek) {
if (auto e = dynamic_cast<Erme*>(p.get())) {
penzek.push_back(make_unique<Erme>(*e));
} else if (auto b = dynamic_cast<Bankjegy*>(p.get())) {
penzek.push_back(make_unique<Bankjegy>(*b));
}
}
}
Penztarca& operator=(const Penztarca& other) {
if (this == &other) return *this;
penzek.clear();
for (const auto& p : other.penzek) {
if (auto e = dynamic_cast<Erme*>(p.get())) {
penzek.push_back(make_unique<Erme>(*e));
} else if (auto b = dynamic_cast<Bankjegy*>(p.get())) {
penzek.push_back(make_unique<Bankjegy>(*b));
}
}
return *this;
}
Penztarca& operator<<(const Penz& p) {
if (auto e = dynamic_cast<const Erme*>(&p)) {
penzek.push_back(make_unique<Erme>(*e));
} else if (auto b = dynamic_cast<const Bankjegy*>(&p)) {
penzek.push_back(make_unique<Bankjegy>(*b));
}
return *this;
}
unsigned osszeg() const {
unsigned sum = 0;
for (const auto& p : penzek) {
sum += p->getErtek();
}
return sum;
}
void kiszor() {
auto it = remove_if(penzek.begin(), penzek.end(), [](const unique_ptr<Penz>& p) {
return dynamic_cast<Erme*>(p.get()) != nullptr;
});
penzek.erase(it, penzek.end());
}
friend ostream& operator<<(ostream& os, const Penztarca& pt) {
for (const auto& p : pt.penzek) {
os << p->toString() << endl;
}
return os;
}
};
#ifndef TEST_BIRO
int main() {
Penztarca pt;
pt << Erme(100, "arany", 20) << Bankjegy(5000, "Szent Istvan") << Erme(50, "ezust", 15);
cout << pt;
cout << "Osszeg: " << pt.osszeg() << endl;
pt.kiszor();
cout << "Kiszoritas utan:" << endl << pt;
}
#endif