Yeah, my code had some syntax issues. Sorry. I was trying to get something up before leaving for home. But I hope I was able to get the idea across. I figured the code I showed would give you the pattern for adding parameters to constructor calls.
(In my defense, I considered the destructor just a declaration, with an expected .cpp file to define it, but, yes the Goblin constructor was missing the {} and had a semicolon when it shouldn't.)
I was trying to show the pattern. As far as the other variables--you can pass them in the constructor, too. so you could define the Enemy constructor as:
Enemy ( string n, int h, int a, int d ) : name(n), health(h), attack(h), defense(d) {}
This would allow each of the derived Enemy classes to define their own health, attack and defense values and assign them at object construction, and they would not have to be the same for all enemies.
1 2
|
Goblin::Goblin() : Enemy("Goblin", 1, 2, 3) {}
Blob::Blob() : Enemy("Blob", 4, 5, 6) {}
|
Of course those derived class constructor definitions have to be in the correct files.
Note: By the way, I was showing the constructors as inline just for simplicity. With inheritance, there can be some issues with inline constructors, especially if you have multiple levels of inheritance and as constructor code is less trivial. One of the standard sets supported by a static analysis tool we have will actually report a warning for an inline constructor. For production code, I would put the constructor body in a .cpp file