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
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item
{
public:
string name;
int weight;
int value;
Item(string n, int w, int v) : name(n), value(v), weight(w) {}
virtual void print(void) {}
};
class Weapon : public Item
{
public:
int damage;
Weapon(int d, string n, int w, int v) :damage(d), Item(n, w, v) {}
void print(void) { std::cout << name << ": " << weight << " " << value << " " << damage << endl; }
};
class Armor : public Item
{
public:
int block;
Armor(int b, string n, int w, int v) :block(b), Item(n,w,v) {};
void print(void) { std::cout << name << ": " << weight << " " << value << " " << block << endl; }
};
class Inventory
{
public:
vector<Item*> items;
};
int main(void)
{
Inventory myInventory;
Item *myFirstWeapon = new Weapon(6, "Sword", 5, 10);
Item *myFirstArmor = new Armor(3, "Shield", 4, 8);
myInventory.items.push_back(myFirstWeapon);
myInventory.items.push_back(myFirstArmor);
for (int i = 0; i < myInventory.items.size(); i++)
myInventory.items[i]->print();
for (int i = 0; i < myInventory.items.size(); i++)
delete myInventory.items[i];
return 0;
}
|