Guest User

Untitled

a guest
Oct 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // darkvee's Polymorphism example
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Enemy
  9. {
  10.     public:
  11.         int flag;
  12.         virtual void attack(){}
  13.  
  14.  
  15.  
  16.  
  17.  
  18. };
  19.  
  20. class Zombie: public Enemy
  21. {
  22.     public:
  23.         void attack()
  24.         {
  25.  
  26.             cout << "zombie takes a bite!" << endl;
  27.         }
  28.  
  29.  
  30.  
  31.  
  32. };
  33.  
  34. class Vampire: public Enemy
  35. {
  36.     public:
  37.         void attack()
  38.         {
  39.  
  40.             cout << "vampire sucks your blood!"  << endl;
  41.         }
  42.  
  43.  
  44. };
  45.  
  46. class Mummy: public Enemy
  47. {
  48.     public:
  49.         void attack()
  50.         {
  51.  
  52.             cout << "mummy drains your soul!" << endl;
  53.         }
  54.  
  55. };
  56.  
  57. class Boogeyman: public Enemy
  58. {
  59.     public:
  60.         void attack()
  61.         {
  62.  
  63.             cout << "boogeyman scares you to death!"  << endl;
  64.  
  65.         }
  66.  
  67.  
  68. };
  69.  
  70.  
  71. class Hero:public Enemy
  72. {
  73.     public:
  74.  
  75.         //myTest 100;
  76.  
  77.         Hero()
  78.  
  79.         {
  80.             health = 100;
  81.  
  82.  
  83.         }
  84.  
  85.         void attackHero()
  86.         {
  87.             switch(flag)
  88.             {
  89.                 case 1:
  90.                     health -= 5;
  91.                 break;
  92.  
  93.                 case 2:
  94.                     health -= 15;
  95.                 break;
  96.  
  97.                 case 3:
  98.                     health -= 25;
  99.                 break;
  100.  
  101.                 case 4:
  102.                     health -= 55;
  103.                 break;
  104.             }
  105.  
  106.             if (health > 0)
  107.             {
  108.  
  109.                 cout << "Hero's health " << getHealth() << endl;
  110.             }
  111.             else
  112.                 cout << "Hero is dead :( " << getHealth() << endl;
  113.  
  114.             flag = 0;
  115.         }
  116.  
  117.  
  118.         int getHealth()
  119.         {
  120.             return health;
  121.         }
  122.  
  123.  
  124.  
  125.  
  126.     private:
  127.         int health;
  128.  
  129. };
  130.  
  131.  
  132.  
  133. int main()
  134. {
  135.     int x;
  136.     x = 0;
  137.     // create objects
  138.     Hero      h;
  139.     Zombie    z;
  140.     Vampire   v;
  141.     Mummy     m;
  142.     Boogeyman b;
  143.  
  144.     // show health before monsters attack hero
  145.  
  146.     // pointer objects
  147.     Enemy *enemy1 = &z;
  148.     Enemy *enemy2 = &v;
  149.     Enemy *enemy3 = &m;
  150.     Enemy *enemy4 = &b;
  151.  
  152.     // point to each attack function
  153.     enemy1->attack();
  154.     h.flag = 1;
  155.     h.attackHero();
  156.  
  157.  
  158.     enemy2->attack();
  159.     h.flag = 2;
  160.     h.attackHero();
  161.     //h.showHeroHealth();
  162.  
  163.     enemy3->attack();
  164.     h.flag = 3;
  165.     h.attackHero();
  166.     //h.showHeroHealth();
  167.  
  168.     enemy4->attack();
  169.     h.flag = 4;
  170.     h.attackHero();
  171.     //h.showHeroHealth();
  172.  
  173.     // panic text
  174.     cout << "All enemies are attacking! Help!!!" << endl;
  175.     return 0;
  176. }
Add Comment
Please, Sign In to add comment