Question: Note: No documentation ( comments ) required on this project. It's messy that in the Balrog class's getDamage ( ) function and the Cyberdemon class's
Note: No documentation comments required on this project.
It's messy that in the Balrog class's getDamage function and the Cyberdemon class's getDamage function we have to write the name of the species before calling the Demon class's getDamage function. It would be better if the Demon class's getDamage function could print the name of the species. Taking this a step further, it would be even better if we didn't have to repeat the cout statement "The attacks for points!" in every class's getDamage function. It would be better if that cout statement could occur just once, in the Creature class's getDamage function.
In the Creature class's getDamage function, insert the following statement:
cout "The getSpecies attacks for damage points!" endl;
Delete or if you prefer, comment out the similar cout statements that appear in the getDamage function of each of the derived classes. There will be one such cout statement to delete in each of the getDamage functions.
Try executing the program. The results won't be quite what we were hoping for.
Now make the getSpecies function in the Creature class a virtual function, and execute the program again. The results will now be correct.
We can now simplify our derived classes even further. Two of the five derived classes have getDamage functions that do nothing more than call their parent class's getDamage function. Delete these two functions. Dont forget to delete both the prototype in the class declaration and the definition. We don't need them, because they can just inherit the getDamage function from the Creature class.
You may have noticed that the Creature class's getSpecies function never gets called. However, it is absolutely critical that any class that is derived from the Creature class define a getSpecies function since that function is called from the Creature class's getDamage function. The best way to implement this is to make the Creature class's getSpecies function a pure virtual function, so that every class that is derived from the Creature class will be required to implement a getSpecies function. Make the Creature class's getSpecies function a pure virtual function.
Comment out the getSpecies function in the Human class and try compiling the program to see what happens. then uncomment it ie return it to it's previous state
Make getDamage a virtual function. This will be important so that in your "battle function see below you can say "CreaturegetDamage and the damage will automatically be calculated for the correct Creature. Note that the parameters for "battle will be of type "Creature" and they will need to be passbyreference. You might try making them passbyvalue to see what happens.
Create a new client program discard the client program from part of the assignment Make a function in your client program that is called from main and has the following prototype:
void battleCreature &Creature Creature& Creature;
Note the parameters are passbyreference because the Creature class is an abstract class, so objects of type "Creature" cannot be declared. The function should calculate the damage done by Creature subtract that amount from Creatures hitpoints, and vice versa. When I say "subtract that amount from Creatures hitpoints, I mean that the actual hitpoints data member of the Creature object will be modified. Also note that this means that both attacks are happening simultaneously; that is if Creature dies because of Creatures attack, Creature still gets a chance to attack back. At the end of each round, the function should print the total hitpoints remaining for each Creature. If at the end of a round, both Creatures end up with or fewer hitpoints, then the battle results in a tie. Otherwise, at the end of a round, if one Creature has positive hitpoints but the other does not, the Creature with positive hitpoints is the winner. The function should loop until the battle is over.
Note that you'll need to reset the creature's hitpoints after each battle.
To start, you may want to use the following very simple main function to test your battle function:
int main
srandstaticcasttimenullptr;
Elf e;
Balrog b;
battlee b;
Once your battle function is working, test it with the given main below. In this main every Creature has a battle with every other Creature. To do this, we first declare four objects, one each of type Elf, Balrog, Human, and Cyberdemon. Then we declare an array of type Creature with elements, and make each array element point to one of the four objects. The array elements will need to be pointerstoCreatures rather than simply Creatures because Creature is an abstract class. Then we use nested for loops to call battle with every possible combination of Creatures.
Use these constant definitions:
const int NUMCREATURES ;
const int ELFDEFAULTSTRENGTH ;
const int BALROGDEFAULTSTRENGTH ;
const int HUMANDEFAULTSTRENGTH ;
const int CYBERDEMONDEFAULTSTRENGTH ;
const int DEFAULTHITPOINTS ;
Use this main function:
int main
srandstaticcasttimenullptr;
Elf eELFDEFAULTSTRENGTH, DEFAULTHITPOINTS;
Balrog bBALROGDEFAULTSTRENGTH, DEFAULTHITPOINTS;
Human hHUMANDEFAULTSTRENGTH, DEFAULTHITPOINTS;
Cyberdemon cCYBERDEMONDEFAULTSTRENGTH, DEFAULTHITPOINTS;
Creature creatures&b &e &c &h;
for int i ; i NUMCREATURES; i
for int j i ; j NUMCREATURES; j
battlecreaturesicreaturesj;
Here is a sample output. As you know, you'll have to pay close attention to spacing, etc., in order to get your program to pass the zyBooks tests.
Battle between the balrog and the elf!!
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The elf attacks for points!
The balrog has hitpoints.
The elf has hitpoints.
The balrog attacks for points!
Demonic attack inflicts additional damage points!
Balrog speed attack inflicts additional damage points!
The elf attacks for points!
The balrog has hitpoints.
The elf has hitpoints.
The balrog wins!
Battle between the balrog and the cyberdemon!!
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The cyberdemon attacks for points!
The balrog has hitpoints.
The cyberdemon has hitpoints.
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The cyberdemon attacks for points!
The balrog has hitpoints.
The cyberdemon has hitpoints.
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The cyberdemon attacks for points!
Demonic attack inflicts additional damage points!
The balrog has hitpoints.
The cyberdemon has hitpoints.
The cyberdemon wins!
Battle between the balrog and the human!!
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The human attacks for points!
The balrog has hitpoints.
The human has hitpoints.
The balrog attacks for points!
Balrog speed attack inflicts additional damage points!
The human attacks for points!
The balrog has hitpoints.
The human has hitpoints.
The human wins!
Battle between the elf and the cyberdemon!!
The elf attacks for points!
The cyberdemon attacks for points!
The elf has hitpoints.
The cyberdemon has hitpoints.
The elf attacks for points!
The cyberdemon attacks for points!
The elf has hitpoints.
The cyberdemon has hitpoints.
The cyberdemon wins!
Battle between the elf and the human!!
The elf attacks for points!
Magical attack inflicts additional damage points!
The human attacks for points!
The elf has hitpoints.
The human has hitpoints.
The elf attacks for points!
The human attacks for points!
The elf has hitpoints.
The human has hitpoints.
The battle results in a tie!
Battle between the cyberdemon and the human!!
The cyberdemon attacks for points!
Demonic attack inflicts additional damage points!
The human attacks for points!
The cyberdemon has hitpoints.
The human has hitpoints.
The cyberdemon wins!
Special zyBooks submission note: To make this work with zyBooks, you will need to comment out the "srand line before submitting.
All of the classes should still be in the cscreature namespace
What's the Point?
Take some time to reflect on the program you just completed from the perspective of the base class, Creature. With respect to inheritance, you'll see categories of member functions illustrated not counting constructors, since they are not inherited
The accessors and mutators are typical member functions that are inherited and never redefined. For example, the balrog class's getDamage function calls getStrength which it can do because it has been inherited from the Creature class via the Demon class
getSpecies is a pure virtual function. It is not defined in the Creature class, but it must be defined in every class that is derived from the Creature class, or the Creature class won't work, because the Creature class's getDamage function depends on the fact that getSpecies exists in the derived class.
getDamage is a virtual member function that is inherited and is sometimes redefined. write all the code h and cpp
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
