Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- enum Type { ANY=0, CAT=1, DOG=2};
- class Animal {
- protected:
- Type type = Type::ANY;
- std::string name;
- public:
- Type getType() {return type;};
- const std::string getName() { return name; }
- };
- static std::list<Animal> animals;
- static Animal dequeue(Type _type = Type::ANY) {
- if( _type == Type::ANY ) {
- Animal a = animals.front();
- animals.pop_front();
- return a;
- } else {
- for (auto it = animals.begin(); it != animals.end(); ) {
- if( (it->getType() == Type::DOG && _type == Type::DOG) ||
- (it->getType() == Type::CAT && _type == Type::CAT)) {
- animals.erase(it);
- return *it;
- } else {
- ++it;
- }
- }
- }
- return Animal();
- }
- class Dog : public Animal {
- public:
- Dog(std::string _name) {
- name = _name;
- type = Type::DOG;
- animals.push_back(*this);
- }
- };
- class Cat : public Animal {
- public:
- Cat(std::string _name) {
- name = _name;
- type = Type::CAT;
- animals.push_back(*this);
- }
- };
- void enqueue(Type _type, std::string _name) {
- if(_type == Type::ANY) {
- std::cout << "Sorry! you have eyes use them" << std::endl;;
- } else if(_type == Type::DOG){
- Dog dog(_name);
- } else {
- Cat cat(_name);
- }
- }
- int main(void) {
- enqueue(Type::DOG,"Joe");
- enqueue(Type::CAT, "Jee");
- Cat bess("bess");
- Dog kalb("kalb");
- Animal animal = dequeue(Type::CAT);
- std::cout << "dequeue name: " << animal.getName() << std::endl;
- animal = dequeue(Type::CAT);
- std::cout << "dequeue name: " << animal.getName() << std::endl;
- }
Add Comment
Please, Sign In to add comment