-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.cpp
70 lines (62 loc) · 1.72 KB
/
builder.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 <string>
enum class HairType {
Bold, Short, Medium, Long
};
enum class HairColor {
None = 0, Red, Brunette, Blonde
};
class HeroBuilder {
public:
HeroBuilder(std::string name, int health) : name(std::move(name)), health(health) {}
HeroBuilder &withHairColor(HairColor color) {
this->hairColor = color;
return *this;
}
HeroBuilder &withHairType(HairType hairType) {
this->hairType = hairType;
return *this;
}
HeroBuilder &withArmor(int armor) {
this->armor = armor;
return *this;
}
HeroBuilder &withMana(int mana) {
this->armor = mana;
return *this;
}
std::string name;
HairColor hairColor = HairColor::None;
HairType hairType = HairType::Bold;
int armor = 0;
int health = 0;
int mana = 0;
};
struct Hero {
public:
explicit Hero(const HeroBuilder &builder) : name(builder.name),
hairColor(builder.hairColor),
hairType(builder.hairType),
armor(builder.armor),
health(builder.health),
mana(builder.mana) {}
friend std::ostream &operator<<(std::ostream &os, const Hero &hero) {
os << "Hero name: " << hero.name << ", Armor: " << hero.armor << ", Health: " << hero.health << ", Mana: "
<< hero.mana << ";";
return os;
}
private:
std::string name;
HairColor hairColor;
HairType hairType;
int armor;
int health;
int mana;
};
int main() {
auto builder = HeroBuilder("Kevin", 100);
builder.withArmor(10).withMana(10).withHairColor(HairColor::Brunette).withHairType(HairType::Medium);
auto hero = Hero(builder);
std::cout << hero << std::endl;
return 0;
}