1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <string>
using namespace std;
class Weapon
{
private:
int attack;
string name;
public:
Weapon(const string & n, int a): name(n), attack(a)
{
}
const string & get_name()
{
return name;
}
int get_attack()
{
return attack;
}
};
class Sword: public Weapon
{
private:
int sharpness;
public:
Sword(const string & n, int a, int s): Weapon(n,a), sharpness(s)
{
}
};
class MagicAttack: public Weapon
{
private:
int MpCost;
public:
MagicAttack(const string & n, int a, int mpc): Weapon(n,a), MpCost(mpc)
{}
};
int main()
{
Weapon* wsword = new Sword("Red Sword", 15, 5);
Weapon* wmagic = new MagicAttack("Lightning", 45, 200);
Weapon* weapons[2] = {wsword, wmagic};
for(int i = 0; i <2; i++)
{
cout << weapons[i]->get_name() << ": " << weapons[i]->get_attack() << endl;
}
delete wsword;
delete wmagic;
}
|