Last active
August 14, 2021 22:30
-
-
Save jstaursky/a6ee9fc2ffcce4e12eabf5fd14ef7191 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
class Exhibit; | |
class Season; | |
class Zoo { | |
public: | |
Zoo(); | |
virtual ~Zoo(); | |
Exhibit* habitat; | |
}; | |
enum class ANIMAL { ELEPHANT, POLAR_BEAR }; | |
class Exhibit { | |
public: | |
Exhibit(); | |
virtual ~Exhibit(); | |
void update_display(); | |
Season* calendar; | |
ANIMAL animal; | |
}; | |
class Season { | |
public: | |
Season() {} | |
virtual ~Season() {} | |
virtual Season* end_season (Exhibit*) = 0; | |
virtual ANIMAL get_animal () = 0; | |
}; | |
class Summer : public Season { | |
public: | |
Summer() {} | |
Season* end_season (Exhibit*); | |
ANIMAL get_animal () { return ANIMAL::ELEPHANT; } | |
}; | |
class Winter : public Season { | |
public: | |
Winter() {} | |
Season* end_season (Exhibit*); | |
ANIMAL get_animal () { return ANIMAL::POLAR_BEAR; } | |
}; | |
Zoo::Zoo() { habitat = new Exhibit(); } | |
Zoo::~Zoo() { delete habitat; } | |
Exhibit::Exhibit() { calendar = new Summer(); } | |
Exhibit::~Exhibit() { delete calendar; } | |
Season* Summer::end_season(Exhibit* display) { | |
return new Winter(); | |
} | |
Season* Winter::end_season(Exhibit* display) { | |
return new Summer(); | |
} | |
void Exhibit::update_display() { | |
Season* date = nullptr; | |
if (date = calendar->end_season(this)) { | |
animal = date->get_animal(); | |
delete calendar; | |
calendar = date; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
Zoo zoo; | |
zoo.habitat->update_display(); | |
if (zoo.habitat->animal == ANIMAL::ELEPHANT) | |
std::cout << "Elephant"; | |
if (zoo.habitat->animal == ANIMAL::POLAR_BEAR) | |
std::cout << "Polar Bear"; | |
std::cout << std::endl; | |
(zoo.habitat)->update_display(); | |
if (zoo.habitat->animal == ANIMAL::ELEPHANT) | |
std::cout << "Elephant"; | |
if (zoo.habitat->animal == ANIMAL::POLAR_BEAR) | |
std::cout << "Polar Bear"; | |
std::cout << std::endl; | |
return 0; | |
} | |
// will print: | |
// | |
// Polar Bear | |
// Elephant |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment