Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Last active August 14, 2021 22:30
Show Gist options
  • Save jstaursky/a6ee9fc2ffcce4e12eabf5fd14ef7191 to your computer and use it in GitHub Desktop.
Save jstaursky/a6ee9fc2ffcce4e12eabf5fd14ef7191 to your computer and use it in GitHub Desktop.
#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