Inheritance Help w/ Static Protected Int in Class

Read comments below. I am trying to learn and understand inheritance & polymorphism.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//Polymorphism practice - create quick polymorphic class with 'break-down' function

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class destructibleObject
{
public:
    virtual void death(){}
    int initializeHealth(int x)
    {
        health = x;
        return health;
    }
    string initializeName(string x)
    {
        name = x;
        return name;
    }
    void findHealth()
    {
        cout << health;
    }
    static void initializeObject(int x)
    {
        objectCount = x;
    }
    void damageObject(int x)
    {
        if (health-x <= 0)
        {
            death();
            delete this;
        }
        else
        {
            health -= 3;
            cout << name << "'s remaining health: " << health << endl;
        }
    }
    destructibleObject(int healthInput)
    {
        initializeHealth(healthInput);
        objectCount = 0;
        cout << "Destructible object initialization complete" << endl;
    }
protected:
    static int 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;
    }
}
Topic archived. No new replies allowed.