Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. const int size = 8;
  7.  
  8. class FOOD {
  9. public:
  10. int price;
  11. FOOD() : price(0) {}
  12. FOOD(int price) { setprice(price); }
  13. ~FOOD() {}
  14. void setprice(int);
  15. };
  16.  
  17. void FOOD::setprice(int price) {
  18. this->price = price;
  19. }
  20.  
  21. class ANIMAL {
  22. protected:
  23. char *name;
  24. public:
  25. int amount;
  26. ANIMAL() : name(NULL) {}
  27. ANIMAL(char *name) { setname(name); }
  28. ANIMAL(const ANIMAL &src) { setname(src.name); }
  29. virtual ~ANIMAL() {};
  30. char * getname();
  31. void setname(char *);
  32. virtual void show(void) = 0;
  33. static void print(void);
  34. void add();
  35. static int count;
  36. static ANIMAL **animals;
  37. static ANIMAL *begin;
  38. };
  39.  
  40. char * ANIMAL::getname() { return this->name; }
  41.  
  42. void ANIMAL::setname(char *name) {
  43. int n = strlen(name);
  44. if (n > 0) {
  45. this->name = (char *)calloc(strlen(name), sizeof(char));
  46. if (this->name != NULL) strcpy(this->name, name);
  47. }
  48. }
  49.  
  50. void ANIMAL::add() {
  51. if (count < size) {
  52. animals[count++] = this;
  53. if (count < size) animals[count] = NULL;
  54. }
  55. }
  56.  
  57. void ANIMAL::print() {
  58. printf("\nCount of animals types: %d\n", count);
  59. for (int i = 0; i < count; i++)
  60. if (animals[i] != NULL) animals[i]->show();
  61. }
  62.  
  63. int ANIMAL::count = 0;
  64.  
  65. ANIMAL ** ANIMAL::animals = new ANIMAL *[size];
  66. ANIMAL * ANIMAL::begin = ANIMAL::animals[0];
  67.  
  68. class MAMMAL : public ANIMAL {
  69. public:
  70. MAMMAL() : ANIMAL(NULL) {}
  71. MAMMAL(char *name, int amount) : ANIMAL(name) { this->amount = amount; }
  72. ~MAMMAL() { free(name); }
  73. void show(void);
  74. };
  75.  
  76. class BIRD : public ANIMAL {
  77. public:
  78. BIRD() : ANIMAL(NULL) {}
  79. BIRD(char *name, int amount) : ANIMAL(name) { this->amount = amount; }
  80. ~BIRD() { free(name); }
  81. void show(void);
  82. };
  83.  
  84. class HOOFED : public MAMMAL {
  85. public:
  86. HOOFED() : MAMMAL(NULL, NULL) {}
  87. HOOFED(char *name, int amount) : MAMMAL(name, amount) { this->amount = amount; }
  88. ~HOOFED() { free(name); }
  89. void show(void);
  90. };
  91.  
  92.  
  93. void MAMMAL::show() { printf("MAMMAL: %s\n", name); }
  94.  
  95. void BIRD::show() { printf("BIRD: %s\n", name); }
  96.  
  97. void HOOFED::show() { printf("HOOFED: %s\n", name); }
  98.  
  99.  
  100. int main() {
  101. char* animal = new char[10];
  102. strcpy(animal, "Cat");
  103. MAMMAL *cat = new MAMMAL(animal, 10);
  104.  
  105. strcpy(animal, "Crow");
  106. BIRD *crow = new BIRD(animal, 25);
  107.  
  108. strcpy(animal, "Elk");
  109. HOOFED *elk = new HOOFED(animal, 2);
  110.  
  111. FOOD mammals_food(200);
  112. FOOD birds_food(50);
  113. FOOD hoofeds_fod(mammals_food.price * 5);
  114.  
  115. cat->add();
  116. crow->add();
  117. elk->add();
  118.  
  119. ANIMAL::print();
  120.  
  121. // ѕодсчет стоимости содержани¤ животных
  122. printf("\nRation value: %d\n", cat->count * mammals_food.price +
  123. crow->count * birds_food.price + elk->count * hoofeds_fod.price);
  124.  
  125. delete elk;
  126. delete crow;
  127. delete cat;
  128. delete animal;
  129.  
  130. system("pause");
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement