«конверт» с несколькими «письмами»

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <vector>
using std::cout;
using std::endl;
enum
{
FIRE = 0x01,
WIND = 0x02,
LIGHTNING = 0x04,
SOIL = 0x08,
WATER = 0x10
};
class Skill // aka Jutsu =)
{
public:
// envelope constructor (see below)
Skill(int _type) throw (std::logic_error);
// destructor
virtual ~Skill()
{
if (mLetters.size() != 0)
{
cout << "~Skill()\t<- envelope destructor" << endl;
// virtual call in destructor!
erase();
}
else
{
cout << "~Skill()\t<- letter destructor" << endl;
}
for (size_t i = 0; i < mLetters.size(); i++)
delete mLetters[i]; // delete Letter for Envelope
// delete 0 for Letter
}
virtual void cast() const
{
for (size_t i = 0; i < mLetters.size(); i++)
mLetters[i]->cast();
}
virtual void show() const
{
for (size_t i = 0; i < mLetters.size(); i++)
mLetters[i]->show();
}
virtual void erase()
{
for (size_t i = 0; i < mLetters.size(); i++)
mLetters[i]->erase();
}
protected:
// letter constructor
Skill() : mLetters(NULL) { cout << "Skill()\t\t<- letter constructor" << endl; }
private:
Skill(const Skill &);
Skill & operator= (Skill &);
std::vector<Skill*> mLetters; // vector of pointers to letters
};
class FireSkill : public Skill
{
public:
~FireSkill() { cout << "~FireSkill()" << endl; }
virtual void cast() const { cout << "Katon!" << endl; }
virtual void show() const { cout << "FireSkill::show()" << endl; }
virtual void erase() { cout << "FireSkill:erase()" << endl; }
private:
friend class Skill;
FireSkill() {}
FireSkill(const FireSkill &);
FireSkill & operator=(FireSkill &);
};
class WoodSkill : public Skill
{
public:
~WoodSkill() { cout << "~WoodSkill()" << endl; }
virtual void cast() const { cout << "Mokuton!" << endl; }
virtual void show() const { cout << "WoodSkill::show()" << endl; }
virtual void erase() { cout << "WoodSkill::erase()" << endl; }
private:
friend class Skill;
WoodSkill() {}
WoodSkill(const WoodSkill &);
WoodSkill & operator=(WoodSkill &);
};
Skill::Skill(int _type) throw (std::logic_error)
{
cout << "Skill(_type)\t<- envelope constructor" << endl;
switch (_type)
{
case FIRE:
mLetters.push_back(new FireSkill);
break;
case SOIL | WATER:
mLetters.push_back(new WoodSkill);
break;
case FIRE | SOIL | WATER:
mLetters.push_back(new FireSkill);
mLetters.push_back(new WoodSkill);
break;
// ...
default:
throw std::logic_error("Incorrect type of element");
}
// virtual call in constructor!
cast();
}
int main()
{
std::vector<Skill*> skills;
try
{
skills.push_back(new Skill(FIRE));
skills.push_back(new Skill(SOIL | WATER));
skills.push_back(new Skill(FIRE | SOIL | WATER));
}
catch (std::logic_error le)
{
std::cerr << le.what() << endl;
return EXIT_FAILURE;
}
for (size_t i = 0; i < skills.size(); i++)
{
skills[i]->show();
delete skills[i];
}
return EXIT_SUCCESS;
}