Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <string>
#include <iostream>
class Actor {
public:
Actor(const char* name) : slapped_(false), name_(name) {}
virtual ~Actor() {}
virtual void update() = 0;
void reset() { slapped_ = false; }
void slap() {
slapped_ = true;
}
bool wasSlapped() { return slapped_; }
const std::string& name() const { return name_; }
private:
bool slapped_;
std::string name_;
};
class Stage {
public:
void add(Actor* actor, int index) {
actors_[index] = actor;
}
void update() {
for (int i = 0; i < NUM_ACTORS; ++i) {
std::cout << "Stage updates actor " << i << " (" << actors_[i]->name() << ")\r\n";
actors_[i]->update();
actors_[i]->reset();
}
std::cout << "Stage updates ends\r\n";
}
private:
static const int NUM_ACTORS = 3;
Actor* actors_[NUM_ACTORS];
};
class Comedian : public Actor {
public:
Comedian(const char* name) : Actor(name) {}
void face(Actor* actor) { facing_ = actor; }
virtual void update() {
if (wasSlapped()) {
std::cout << " " << name() << " was slapped, so he slaps " << facing_->name() << std::endl;
facing_->slap();
}
}
private:
Actor* facing_;
};
int _tmain(int argc, _TCHAR* argv[]) {
Stage stage;
Comedian* harry = new Comedian("Harry");
Comedian* baldy = new Comedian("Baldy");
Comedian* chump = new Comedian("Chump");
harry->face(baldy);
baldy->face(chump);
chump->face(harry);
stage.add(harry, 0);
stage.add(baldy, 1);
stage.add(chump, 2);
harry->slap();
stage.update();
int temp = 0;
std::cin >> temp;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.