Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // darkvee's Polymorphism example
- #include <iostream>
- using namespace std;
- class Enemy
- {
- public:
- int flag;
- virtual void attack(){}
- };
- class Zombie: public Enemy
- {
- public:
- void attack()
- {
- cout << "zombie takes a bite!" << endl;
- }
- };
- class Vampire: public Enemy
- {
- public:
- void attack()
- {
- cout << "vampire sucks your blood!" << endl;
- }
- };
- class Mummy: public Enemy
- {
- public:
- void attack()
- {
- cout << "mummy drains your soul!" << endl;
- }
- };
- class Boogeyman: public Enemy
- {
- public:
- void attack()
- {
- cout << "boogeyman scares you to death!" << endl;
- }
- };
- class Hero:public Enemy
- {
- public:
- //myTest 100;
- Hero()
- {
- health = 100;
- }
- void attackHero()
- {
- switch(flag)
- {
- case 1:
- health -= 5;
- break;
- case 2:
- health -= 15;
- break;
- case 3:
- health -= 25;
- break;
- case 4:
- health -= 55;
- break;
- }
- if (health > 0)
- {
- cout << "Hero's health " << getHealth() << endl;
- }
- else
- cout << "Hero is dead :( " << getHealth() << endl;
- flag = 0;
- }
- int getHealth()
- {
- return health;
- }
- private:
- int health;
- };
- int main()
- {
- int x;
- x = 0;
- // create objects
- Hero h;
- Zombie z;
- Vampire v;
- Mummy m;
- Boogeyman b;
- // show health before monsters attack hero
- // pointer objects
- Enemy *enemy1 = &z;
- Enemy *enemy2 = &v;
- Enemy *enemy3 = &m;
- Enemy *enemy4 = &b;
- // point to each attack function
- enemy1->attack();
- h.flag = 1;
- h.attackHero();
- enemy2->attack();
- h.flag = 2;
- h.attackHero();
- //h.showHeroHealth();
- enemy3->attack();
- h.flag = 3;
- h.attackHero();
- //h.showHeroHealth();
- enemy4->attack();
- h.flag = 4;
- h.attackHero();
- //h.showHeroHealth();
- // panic text
- cout << "All enemies are attacking! Help!!!" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment