//Polymorphism practice - create quick polymorphic class with 'break-down' function
#include <iostream>
#include <string>
#include <typeinfo>
usingnamespace std;
class destructibleObject
{
public:
virtualvoid death(){}
int initializeHealth(int x)
{
health = x;
return health;
}
string initializeName(string x)
{
name = x;
return name;
}
void findHealth()
{
cout << health;
}
staticvoid initializeObject(int x)
{
objectCount = x;
}
void damageObject(int x)
{
if (health-x <= 0)
{
death();
deletethis;
}
else
{
health -= 3;
cout << name << "'s remaining health: " << health << endl;
}
}
destructibleObject(int healthInput)
{
initializeHealth(healthInput);
objectCount = 0;
cout << "Destructible object initialization complete" << endl;
}
protected:
staticint objectCount;
//The problem is that I cannot set a static int objectCount, initialize it, and then have a new static objectCount variable passed to each derived class.
//My question is essentially, how do I create a static objectCount class where each derived class has its own instance of objectCount?
int health = 0;
string name = "default";
};
class postBox: public destructibleObject
{
public:
void death()
{
cout << "The postbox falls apart to a small pile of wood." << endl;
}
postBox(int healthInput): destructibleObject(healthInput)
{
initializeHealth(healthInput);
name = "Post Box";
cout << "A " << name << " was created with a health value of " << health << ".\n\n";
}
};
class tree: public destructibleObject
{
public:
void death()
{
cout << "With a large, large rumble, the tree falls down." << endl;
}
tree(int healthInput): destructibleObject(healthInput)
{
initializeHealth(healthInput);
name = "Tree";
cout << "A " << name << " was created with a health value of " << health << ".\n\n";
}
};
int main()
{
//destructibleObject* test = new destructibleObject(2);
//destructibleObject* test2 = new destructibleObject(3);
postBox* red_box = new postBox(5);
tree* tall_tree = new tree(40);
cout << tall_tree->objectCount;
while (true){
string x;
cin >> x;
if (x == "chop_tree" && tall_tree != nullptr)
tall_tree->damageObject(3);
if (x == "smash_postBox")
red_box->damageObject(3);
if (x == "exit")
return 0;
}
}