fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <string>
  5. #include <stdlib.h>
  6. using namespace std;
  7.  
  8.  
  9. class Animal {
  10. public:
  11. virtual void eat() = 0;
  12. };
  13.  
  14. // Dog is more high-level
  15. class Dog: public Animal {
  16. public:
  17. char attr1;
  18. char attr2;
  19.  
  20. Dog(char _attr1, char _attr2):
  21. attr1(_attr1),
  22. attr2(_attr2)
  23. {}
  24.  
  25. Dog():
  26. attr1('d'),
  27. attr2('g')
  28. {}
  29.  
  30. void eat() {
  31. cout << "Dog ate food (" << attr1 << attr2 << ").\n";
  32. }
  33.  
  34. void barf() {
  35. cout << "Whaf";
  36. }
  37. };
  38.  
  39. // Cat is more low-level
  40. class Cat: public Animal {
  41. public:
  42. // A cat can basically only exist in a cage
  43. // and it's attributes are defined by it's location
  44. // in the Cage's underlying array. A Cat should also
  45. // have to 2 attributes (both char), so this would
  46. // be kind of true:
  47. // sizeof(Cat) == 2
  48.  
  49. char* ptr;
  50.  
  51. // I will only use a Cat in a context of arrays, so
  52. // no need to care about memory management here.
  53. Cat(char* _ptr):
  54. ptr(_ptr)
  55. {}
  56.  
  57. void eat() {
  58. cout << "Cat ate food (" << *ptr << *(ptr+1) << ").\n";
  59. }
  60.  
  61. void meow() {
  62. cout << "Meow.";
  63. }
  64. };
  65.  
  66.  
  67. class Cage {
  68. public:
  69. virtual Animal* GetRandomAnimal() = 0;
  70. };
  71.  
  72. // DogCage uses a nice (more high level) vector
  73. class DogCage: public Cage {
  74. public:
  75. vector<Dog> dogs;
  76.  
  77. DogCage():
  78. dogs(5)
  79. {
  80. dogs[0] = Dog();
  81. }
  82.  
  83. Animal* GetRandomAnimal() {
  84. return &dogs[0];
  85. }
  86. };
  87.  
  88. // CatCage uses a more low level pointer together with
  89. // malloc etc.
  90. class CatCage: public Cage {
  91. public:
  92. char* cats;
  93.  
  94. CatCage():
  95. cats((char*) malloc(4*2)) // room for 4 cats
  96. {
  97. *cats = 'c';
  98. *(cats+1) = 't';
  99. }
  100.  
  101. ~CatCage() {
  102. free(cats);
  103. }
  104.  
  105. Animal* GetRandomAnimal() {
  106. int random = 0;
  107.  
  108. // This is why it's difficult to return a pointer
  109. // to an Animal. I basically want to use a Cat as
  110. // a simple interface to a part of an array.
  111. // If I use pointers etc, that seems like it would
  112. // hit performance.
  113. Cat* c = new Cat(cats+(random*2));
  114. return c;
  115. }
  116. };
  117.  
  118. void FeedAnimals(Animal& a) {
  119. a.eat();
  120. }
  121.  
  122. int main() {
  123. //Cage cage;
  124. string s;
  125.  
  126. cout << "Cats or dogs? ";
  127. cin >> s;
  128. if (s=="Cats" || s=="cats" || s=="c") {
  129. CatCage cage = CatCage();
  130. FeedAnimals(*(cage.GetRandomAnimal()));
  131. } else {
  132. DogCage cage = DogCage();
  133. FeedAnimals(*(cage.GetRandomAnimal()));
  134. }
  135. }
  136.  
Success #stdin #stdout 0s 3280KB
stdin
cats
stdout
Cats or dogs? Cat ate food (ct).