a couple questions about inventory management system
So I've been messing with polymorphism and inheritance a lot recently. I have been reading everywhere and looking through code of some RPGs and realizing how beneficial it is for RPGs. I have been messing around with some stuff to try and find a way to implement it. I came up with something that I could possibly use for inventory and possibly other things.
Code:
#include <iostream>
#include <string>
using namespace std;
#define ITEM 00
#define WEAPON 01
#define POTION 02
class Item
{
public:
Item(const string& name, const int& type): m_name(name), m_type(type) {};
virtual ~Item() {};
virtual void DoAction() = 0;
protected:
string m_name;
int m_type;
};
class Weapon: public Item
{
public:
Weapon(const string& name, const int& type, const int& dmg): Item(name,type), m_dmg(dmg) {};
virtual ~Weapon() {};
virtual void DoAction();
private:
int m_dmg;
};
void Weapon::DoAction()
{
cout << "The weapon hits the enemy for " << m_dmg << " damage\n";
}
class Potion: public Item
{
public:
Potion(const string& name, const int& type, const int& amt): Item(name, type), m_healamt(amt) {};
virtual ~Potion() {};
virtual void DoAction();
private:
int m_healamt;
};
void Potion::DoAction()
{
cout << "The potions heals the user by " << m_healamt << " health\n";
}
int main()
{
Item* sword = new Weapon("Sword", WEAPON, 34);
Item* healthPotion = new Potion("Health Potion", POTION, 26);
sword->DoAction();
healthPotion->DoAction();
cin.get();
delete sword;
delete healthPotion;
return 0;
}
Granted the weapon and potion are both split classes but i figured that once they were created there would be no reason to access the damage or healing amount. Basically my question is this. Is this a good way to work with inventory? is there a better way out there that wouldn't get ridiculously complex? I'm asking this because of the fact that they are split (in the event that I had to edit the values) and because virtual functions do use up a bit of memory to create. Any information would be appreciated.