Vladislav_Bezruk

lab 10 2 task

Oct 15th, 2021
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Animals { //клас тварин
  6. protected: //тип для доступу у похідних класах
  7.     float weight; //маса
  8.     int lifetime; //тривалість життя
  9.  
  10. public:
  11.     Animals() {} //пустий конструктор
  12.  
  13.     Animals(float a, int b) : weight(a), lifetime(b) {} //констуктор ініціалізації списком
  14.  
  15.     virtual void get() { //віртуальна функція для друкування даних
  16.         cout << "Info about animal:" << endl;
  17.         cout << "\tweight: " << weight << endl;
  18.         cout << "\tlifetime: " << lifetime << endl << endl;
  19.     }
  20.  
  21.     virtual void set() { //віртуальна функція для встановлення даних
  22.         cout << "Enter info about animal:" << endl;
  23.         cout << "\tweight: ";
  24.         cin >> weight;
  25.         cout << "\tlifetime: ";
  26.         cin >> lifetime;
  27.         cout << endl;
  28.     }
  29. };
  30.  
  31. class Mammals : public Animals { //клас ссавці на основі тварин
  32. protected: //тип для доступу у похідних класах
  33.     string coatColor; //колір шерсті
  34.  
  35. public:
  36.     Mammals() {} //пустий констуктор
  37.  
  38.     Mammals(float a, int b, string c) : Animals(a, b), coatColor(c) {} //констуктор ініціалізації списком (ініціалізується також клас тварин)
  39.  
  40.     virtual void get() { //віртуальна функція для друкування даних
  41.         cout << "Info about mammals:" << endl;
  42.         cout << "\tweight: " << weight << endl;
  43.         cout << "\tlifetime: " << lifetime << endl;
  44.         cout << "\tcoat color: " << coatColor << endl << endl;
  45.     }
  46.  
  47.     virtual void set() { //віртуальна функція для встановлення даних
  48.         cout << "Enter info about mammals:" << endl;
  49.         cout << "\tweight: ";
  50.         cin >> weight;
  51.         cout << "\tlifetime: ";
  52.         cin >> lifetime;
  53.         cout << "\tcoat color: ";
  54.         cin >> coatColor;
  55.         cout << endl;
  56.     }
  57. };
  58.  
  59. class Artiodactylas : public Mammals { //клас парнокопитні на основі ссавців
  60.     float runningSpeed; //швидкість пересування
  61.  
  62. public:
  63.     Artiodactylas() {} //пустий констуктор
  64.  
  65.     Artiodactylas(float a, int b, string c, float d) : Mammals(a, b, c), runningSpeed(d) {} //констуктор ініціалізації списком (ініціалізується також клас ссавців)
  66.  
  67.     void get() { //функція для друкування даних
  68.         cout << "Info about artiodactylas:" << endl;
  69.         cout << "\tweight: " << weight << endl;
  70.         cout << "\tlifetime: " << lifetime << endl;
  71.         cout << "\tcoatColor: " << coatColor << endl;
  72.         cout << "\trunning speed: " << runningSpeed << endl << endl;
  73.     }
  74.  
  75.     void set() { //функція для введення даних
  76.         cout << "Enter info about artiodactylas:" << endl;
  77.         cout << "\tweight: ";
  78.         cin >> weight;
  79.         cout << "\tlifetime: ";
  80.         cin >> lifetime;
  81.         cout << "\tcoatColor: ";
  82.         cin >> coatColor;
  83.         cout << "\trunning speed: ";
  84.         cin >> runningSpeed;
  85.         cout << endl;
  86.     }
  87. };
  88.  
  89. class Birds : public Animals { //клас птиць на основі тварин
  90.     string featherColor; //колір пір'я
  91.     float flightSpeed; //швидкість польоту
  92.  
  93. public:
  94.     Birds() {} //пустий констуктор
  95.  
  96.     Birds(float a, int b, string c, float d) : Animals(a, b), featherColor(c), flightSpeed(d) {} //конструктор ініціалізації списком (ініціалізується також клас тварин)
  97.  
  98.     void get() { //фукнція для виведення даних
  99.         cout << "Info about bird:" << endl;
  100.         cout << "\tweight: " << weight << endl;
  101.         cout << "\tlifetime: " << lifetime << endl;
  102.         cout << "\tfeather color: " << featherColor << endl;
  103.         cout << "\tflight speed: " << flightSpeed << endl << endl;
  104.     }
  105.  
  106.     void set() { //функція для введення даних
  107.         cout << "Enter info about bird:" << endl;
  108.         cout << "\tweight: ";
  109.         cin >> weight;
  110.         cout << "\tlifetime: ";
  111.         cin >> lifetime;
  112.         cout << "\tfeather color: ";
  113.         cin >> featherColor;
  114.         cout << "\tflight speed: ";
  115.         cin >> flightSpeed;
  116.         cout << endl;
  117.     }
  118. };
  119.  
  120. int main() {
  121.  
  122.     Animals* a = new Mammals(50, 25, "black"); //інаціалізація за констуктором класу Mammals (ссавці)
  123.  
  124.     a->get(); //функція класу Mammals (ссавці)
  125.  
  126.     Animals* b = new Artiodactylas(55, 30, "gray", 5); //інаціалізація за констуктором класу Artiodactylas (парнокопитні)
  127.  
  128.     b->get(); //функція класу Artiodactylas (парнокопитні)
  129.  
  130.     Animals* c = new Birds(1, 5, "red", 15); //інаціалізація за констуктором класу Birds (птиці)
  131.  
  132.     c->get(); //функція класу Birds (птиці)
  133.  
  134.     delete a, b, c;
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment