Untitled
unknown
plain_text
3 years ago
1.2 kB
10
Indexable
#include <iostream>
using namespace std;
class Enemy {
protected:
int attackPower;
public:
void setAttackPower(int a) {
attackPower = a;
}
};
class Ninja : public Enemy {
public:
void attack() {
cout << "Ninja! - " << attackPower << endl;
}
};
class Monster : public Enemy {
public:
void attack() {
cout << "Monster! - " << attackPower << endl;
}
};
class Defender {
protected:
int attackPower;
public:
void setAttackPower(int power) {
attackPower = power;
}
};
class Sniper : public Defender {
public:
void attack() {
cout << "Sniper! - " << attackPower << endl;
}
};
class Superman : public Defender {
public:
void attack() {
cout << "Superman! - " << attackPower << endl;
}
};
int main() {
Ninja n;
Monster m;
Sniper s;
Superman superman;
Enemy* e1 = &n;
Enemy* e2 = &m;
Defender* e3 = &s;
Defender* e4 = &superman;
e1->setAttackPower(20);
e2->setAttackPower(80);
e3->setAttackPower(50);
e4->setAttackPower(70);
n.attack();
m.attack();
s.attack();
superman.attack();
}Editor is loading...