I am trying to build my first game in c++ and I am attempting to make an Inventory
system that will allow me to manage and arrange different Item
data types within an Inventory
container type. In the code below I only have the class declarations made up but I wanted to know how to make the "Item" class that I have made hold a data type of any of the different inherited items such as the Weapon
, Apparel
, and Aid
categories. Would it be as simple as using a template
parameter? If so then my question is how does something like a Constructor
understand what to do in each instance of creating an item? The default Constructor
may have to be something like a basic weapon that I decide to set up, correct? If anyone has any advice or links to provide that would be greatly appreciated.
Thanks!
ITEM.H:
#ifndef ITEM_H
#define ITEM_H
#include <iostream>
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
class Item
{
public:
private:
};
/*************************************************
* WEAPON CLASS
* *This class is a type of Item that classifies a
* weapon item that can be in an Inventory.
**************************************************/
class Weapon : public Item
{
public:
private:
};
/*************************************************
* APPAREL CLASS
* *This class is a type of Item that classifies an
* apparel item that can be in an Inventory.
**************************************************/
class Apparel : public Item
{
public:
private:
};
/*************************************************
* AID CLASS
* *This class is a type of Item that classifies an
* Aid item that can be in an Inventory.
**************************************************/
class Aid : public Item
{
public:
private:
};
#endif //ITEM_H
EDIT:
Here is what I have as of now:
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
template <class T>
class Item
{
public:
private:
T item_type; //i.e. Weapon, Apparel, Aid, etc...
};
/*****************************************************************
* ITEM : WEAPON
* *Below is the code for all the different Weapon classes.
******************************************************************/
/*************************************************
* WEAPON CLASS
* *This class is a type of Item that classifies a
* weapon item that can be in an Inventory.
**************************************************/
template <class T>
class Weapon : public Item
{
public:
private:
T weapon_type; //i.e. Melee, Ranged, etc...
};
/*************************************************
* MELEE CLASS
* *This class is a type of Item that classifies a
* melee item that will be used as a weapon.
**************************************************/
template <class T>
class Melee : public Weapon
{
public:
private:
};
/*************************************************
* RANGED CLASS
* *This class is a type of Item that classifies a
* ranged item that will be used as a weapon.
**************************************************/
template <class T>
class Ranged : public Weapon
{
public:
private:
};
ANOTHER EDIT: Here is the newest code:
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
template <class T>
class Item
{
public:
protected:
T item_type; //i.e. Weapon, Apparel, Aid, etc...
};
/*****************************************************************
* ITEM : WEAPON
* *Below is the code for all the different Weapon classes.
******************************************************************/
//WEAPON CLASS
template <class T>
class Weapon : public Item
{
public:
protected:
T weapon_type; //i.e. Melee, Ranged, etc...
};
//MELEE CLASS
template <class T>
class Melee : public Weapon
{
public:
protected:
T melee_type; //i.e. Blade, Blunt, Fist, etc...
};
//BLADE CLASS
template <class T>
class Blade : public Melee
{
public:
protected:
T blade_type; //i.e. Dagger, Sword, etc...
};
//DAGGER CLASS
template <class T>
class Dagger : public Blade
{
public:
//Default Constructor...the only Constructor
Dagger() : damage(1), weight(0.5) { }
//getters
std::string getType() const { return "Dagger"; }
protected:
int damage; //amount of damage it can inflict
float weight; //weight of the dagger
};
One solution is to define your types as enum
s.
enum ITEM_TYPE { IT_WEAPON, IT_END };
enum WEAPON_TYPE { WT_MELEE, WT_RANGED, WT_END };
class Item {
public:
Item(ITEM_TYPE type) : item_type(type) {}
protected:
ITEM_TYPE item_type;
};
class Weapon : public Item {
public:
Weapon(WEAPON_TYPE type) : Item(IT_WEAPON), weapon_type(type) {}
protected:
WEAPON_TYPE weapon_type;
};
class MeleeWeapon : public Weapon {
public:
MeleeWeapon() : Weapon(WT_MELEE) {}
};
class RangedWeapon : public Weapon {
public:
RangedWeapon() : Weapon(WT_RANGED) {}
};