Hello,
PHP Code:
//[CODE]
/*-------- Item Class --------*/
class Item
{
protected:
string name;
int price;
int useLevel;
public:
Item(string tname, int tprice, int tuseLevel);
~Item();
string getName();
int getPrice();
int getUseLevel();
virtual char getType();
virtual int getDamage()=0;
virtual bool isTwoHanded()=0;
virtual int getProtection()=0;
};
/*-------- Weapon(Item) Class --------*/
class Weapon : public Item
{
private:
int causeDamage;
bool twoHand;
public:
Weapon(string tname, int tprice, int tuseLevel ,int damage, bool twohands);
~Weapon();
int getDamage();
bool isTwoHanded();
char getType();
};
/*-------- Armor(Item) Class --------*/
class Armor : public Item
{
private:
int protection;
public:
Armor(string tname, int tprice, int tuseLevel ,int tprotection);
~Armor();
int getProtection();
char getType();
};
//[/CODE]
Item class is a base abstract class. Both Weapon and Armor are derived class. As far as i can remember the above is legal, but when the code goes like this
Code:
Item *equipedWeapon = new Weapon("Wooden Stick", 0, 1, 5, false);
Item *equipedArmor = new Armor("Shirt", 0, 1, 2);
MS VS compiler produces :
error C2259: 'Weapon' : cannot instantiate abstract class
error C2259: 'Armor' : cannot instantiate abstract class
Following the error code i came up to this which doesn't explain why i have to define the functions of Armor also in Weapon class and vice versa.