-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.cpp
49 lines (42 loc) · 866 Bytes
/
factory.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
#include <iostream>
#include <string>
struct Currency {
virtual std::string symbol() = 0;
virtual std::string code() = 0;
};
enum class Country {
Spain, France, USA
};
struct Euro: Currency {
std::string symbol() override {
return "€";
}
std::string code() override {
return "EUR";
}
};
struct Dollar: Currency {
std::string symbol() override {
return "$";
}
std::string code() override {
return "USD";
}
};
class CurrencyFactory {
public:
Currency *createCurrency(Country country) {
switch (country) {
case Country::France:
case Country::Spain: return new Euro;
case Country::USA: return new Dollar;
}
}
};
int main() {
auto factory = CurrencyFactory();
auto *currency = factory.createCurrency(Country::France);
std::cout << currency->code() << " – " << currency->symbol() << std::endl;
delete currency;
return 0;
}