SHARE
TWEET

lab23

debosch Feb 17th, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Лабораторная работа №3
  3.     Шильцин Влад
  4.     Придумайте набор предметов (минимум 3),
  5.     у которых можно выделить общие свойства.
  6.     Создайте абстрактный класс, который будет
  7.     описывать придуманные предметы в общем (минимум 3 функции).
  8.     Наследуйте от него классы, которые будут описывать каждый из предметов.
  9.     Покажите, как совершать операции над всеми классами, используя полиморфизм.
  10.  
  11. */
  12.  
  13. #include <iostream>
  14. #include <Windows.h>
  15.  
  16. class Enemy
  17. {
  18. public:
  19.     virtual void hurt(int damage) { printf_s("Enemy gets %d damage...\n", damage); }
  20.     virtual void walk() { std::cout << "Enemy is walking...\n"; }
  21.     virtual void searching() { std::cout << "Enemy is searching...\n"; }
  22.     virtual void attack() { std::cout << "Enemy is attacking...\'n"; }
  23. };
  24.  
  25. class Wyvern : public Enemy
  26. {
  27. public:
  28.     void hurt(int damage) override { printf_s("Wyvern gets %d damage, but resists 20 damage...\n", damage); }
  29.     void walk() override { std::cout << "Wyvern is flying...\n"; }
  30.     void searching() override { std::cout << "Wyvern is searching...\n"; }
  31.     void attack() override { std::cout << "Wyvern is attacking...\n"; }
  32. };
  33.  
  34. class Worm : public Enemy
  35. {
  36. public:
  37.     void hurt(int damage) override { printf_s("Worm gets %d damage, but resists 2 damage...\n", damage); }
  38.     void walk() override { std::cout << "Worm is crawling...\n"; }
  39.     void searching() override { std::cout << "Worm is searching for food...\n"; }
  40.     void attack() override { std::cout << "Worm is trying to attack, but it is impossible...\n"; }
  41. };
  42.  
  43. class Bunny : public Enemy
  44. {
  45. public:
  46.     void hurt(int damage) override { printf_s("Bunny gets %d damage, but resists 2000000000 damage...\n", damage); }
  47.     void walk() override { std::cout << "Bunny is driving a car...\n"; }
  48.     void searching() override { std::cout << "Bunny is searching for fresh meat...\n"; }
  49.     void attack() override { std::cout << "Bunny is attacking and world gets destroyed...\n"; }
  50. };
  51.  
  52. class Player
  53. {
  54. public:
  55.     Player(int damage) { this->damage = damage; }
  56.     void attack(Enemy* enemy) { enemy->hurt(damage); }
  57.  
  58. private:
  59.     int damage;
  60. };
  61.  
  62. int main()
  63. {
  64.     Wyvern wyvern;
  65.     Worm worm;
  66.     Bunny bunny;
  67.     Player* player = new Player(100);
  68.  
  69.     player->attack(&wyvern);
  70.     player->attack(&worm);
  71.     player->attack(&bunny);
  72.     bunny.walk();
  73.  
  74.     return 0;
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top