Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4.  
  5. enum Type { ANY=0, CAT=1, DOG=2};
  6.  
  7.  
  8. class Animal {
  9. protected:
  10. Type type = Type::ANY;
  11. std::string name;
  12. public:
  13. Type getType() {return type;};
  14. const std::string getName() { return name; }
  15. };
  16.  
  17. static std::list<Animal> animals;
  18. static Animal dequeue(Type _type = Type::ANY) {
  19. if( _type == Type::ANY ) {
  20. Animal a = animals.front();
  21. animals.pop_front();
  22. return a;
  23. } else {
  24. for (auto it = animals.begin(); it != animals.end(); ) {
  25. if( (it->getType() == Type::DOG && _type == Type::DOG) ||
  26. (it->getType() == Type::CAT && _type == Type::CAT)) {
  27. animals.erase(it);
  28. return *it;
  29. } else {
  30. ++it;
  31. }
  32. }
  33. }
  34. return Animal();
  35. }
  36.  
  37.  
  38. class Dog : public Animal {
  39. public:
  40. Dog(std::string _name) {
  41. name = _name;
  42. type = Type::DOG;
  43. animals.push_back(*this);
  44. }
  45.  
  46. };
  47. class Cat : public Animal {
  48. public:
  49. Cat(std::string _name) {
  50. name = _name;
  51. type = Type::CAT;
  52. animals.push_back(*this);
  53. }
  54. };
  55.  
  56. void enqueue(Type _type, std::string _name) {
  57. if(_type == Type::ANY) {
  58. std::cout << "Sorry! you have eyes use them" << std::endl;;
  59. } else if(_type == Type::DOG){
  60. Dog dog(_name);
  61. } else {
  62. Cat cat(_name);
  63. }
  64. }
  65.  
  66. int main(void) {
  67.  
  68. enqueue(Type::DOG,"Joe");
  69. enqueue(Type::CAT, "Jee");
  70. Cat bess("bess");
  71. Dog kalb("kalb");
  72.  
  73. Animal animal = dequeue(Type::CAT);
  74. std::cout << "dequeue name: " << animal.getName() << std::endl;
  75. animal = dequeue(Type::CAT);
  76. std::cout << "dequeue name: " << animal.getName() << std::endl;
  77. }
Add Comment
Please, Sign In to add comment