Created
May 18, 2016 13:52
-
-
Save parkpd/8fcb720bec0180103f70748666959bc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} else { | |
std::cout << " " << name() << " was not slapped, so he does nothing\r\n"; | |
} | |
} | |
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, 2); | |
stage.add(baldy, 1); | |
stage.add(chump, 0); | |
harry->slap(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
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