Question: Assignment 7.2 is to be answered based on 7.1 below is the solution for 7.1 thank you. #include #include #include #include using namespace std; class

Assignment 7.2 is to be answered based on 7.1 below is the solution for 7.1 thank you.

#include #include #include #include using namespace std;

class creature { private: int strength;

int hitpoints;

public: virtual string getSpecies() = 0; creature(); creature( int newStrength, int newHitpoints); virtual int getDamage() ; int getStrength(); void setStrength(int newStrength); int getHitpoints(); void setHitPoints(int newHitPoints);

};

class demon : public creature { public: string getSpecies() ; int getDamage() ; demon(); demon(int newStrength, int newHitPoints); };

class elf : public creature { public: string getSpecies() ; int getDamage() ; elf(); elf(int newStrength, int newHitPoints); };

class human : public creature { public: string getSpecies() ; human(); human(int newStrength, int newHitPoints); };

class cyberdemon : public demon { public: string getSpecies() ; cyberdemon(); cyberdemon(int newStrength, int newHitPoints); };

class balrog : public demon { public: string getSpecies() ; int getDamage() ; balrog(); balrog(int newStrength, int newHitPoints); };

string demon::getSpecies() { return "Demon"; } string human::getSpecies() { return "Human"; } string elf::getSpecies() { return "Elf"; } string cyberdemon::getSpecies() { return "Cyberdemon"; } string balrog::getSpecies() { return "Balrog"; } string creature::getSpecies() { return "Creature"; }

int creature::getDamage() { int damage; damage = (rand() % strength) + 1; cout << "The " << getSpecies() << " attacks for "<< damage << " points!" << endl; return damage; }

int demon::getDamage() { int damage = creature::getDamage(); if (rand() % 4 == 0) { damage = damage + 50; cout << "Demonic attack" << endl; }

return damage; } int balrog::getDamage() { cout<<"The balrog"; int damage = demon::getDamage(); int damage2 = (rand() % getStrength()) + 1; damage += damage2; return damage; }

int elf::getDamage() { int damage = creature::getDamage(); cout << getSpecies() << " attacks for " << damage<< " points!" << endl; if ((rand() % 2) == 0) { damage *= 2; }

return damage; }

creature::creature() { strength = 10; hitpoints = 10; } creature::creature(int newStrength, int newHitPoints) { strength = newStrength; hitpoints = newHitPoints; }

demon::demon() : creature() {

} demon::demon(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints) {

}

elf::elf() : creature() {

} elf::elf(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints) {

}

human::human() : creature() {

} human::human(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints) {

}

cyberdemon::cyberdemon() : demon() {

} cyberdemon::cyberdemon(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints) {

}

balrog::balrog() : demon() {

} balrog::balrog(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints) {

} void creature::setHitPoints(int newHitPoints) { hitpoints = newHitPoints; } void creature::setStrength(int newStrength) { strength = newStrength; } int creature::getHitpoints() { return hitpoints; } int creature::getStrength() { return strength; } int battleArena(creature &creature1, creature& creature2) { bool isContinue = true; int damage1 ,damage2; while(isContinue) { damage1 = creature2.getHitpoints() - creature1.getDamage(); damage2 = creature1.getHitpoints() - creature2.getDamage(); if(damage1<0 || damage2<0) { iscontinue = false; return 0;>= 0 && damage1 <3) || (damage2> = 0 && damage2 < 3)) { isContinue = false; return 1;

} }

}

int main() { srand(time(0));

human h1; elf e1; cyberdemon c1; balrog b1;

human h(20, 30); elf e(40, 50); cyberdemon c(60, 70); balrog b(80, 90); cout << "default human strength/hitpoints: " << h1.getStrength() << "/" << h1.getHitpoints() << endl; cout << "default elf strength/hitpoints: " << e1.getStrength() << "/" << e1.getHitpoints() << endl; cout << "default cyberdemon strength/hitpoints: " << c1.getStrength() << "/" << c1.getHitpoints() << endl; cout << "default balrog strength/hitpoints: " << b1.getStrength() << "/" << b1.getHitpoints() << endl; cout << "non-default human strength/hitpoints: " << h.getStrength() << "/" << h.getHitpoints() << endl; cout << "non-default elf strength/hitpoints: " << e.getStrength() << "/" << e.getHitpoints() << endl; cout << "non-default cyberdemon strength/hitpoints: "<< c.getStrength() << "/" << c.getHitpoints()<< endl; cout << "non-default balrog strength/hitpoints: " << b.getStrength() << "/" << b.getHitpoints() << endl; cout << endl << endl; cout << "Examples of " << h.getSpecies() << " damage: " << endl; for (int i = 0; i < 10; i++){ int damage = h.getDamage(); cout << " Total damage = " << damage << endl; cout << endl; } cout << endl;

cout << "Examples of " << e.getSpecies() << " damage: " << endl; for (int i = 0; i < 10; i++) { int damage = e.getDamage(); cout << " Total damage = " << damage << endl; cout << endl; } cout << endl;

cout << "Examples of " << c.getSpecies() << " damage: " << endl; for (int i = 0; i < 10; i++) { int damage = c.getDamage(); cout << " Total damage = " << damage << endl; cout << endl; } cout << endl;

cout << "Examples of " << b.getSpecies() << " damage: " << endl; for (int i = 0; i < 10; i++) { int damage = b.getDamage(); cout << " Total damage = " << damage << endl; cout << endl; } cout << endl; int result = battleArena(b,c); if(result == 0) cout<<"Its a tie !"; else cout<<"Game over !"; system("pause"); }

Assignment 7.2 [45 points]

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.

int main() { srand((time(0))); Elf e(50,50); Balrog b(50,50);; battleArena(e, b); } 

Make sure that when you test your classes you see examples of the Elf doing a magical attack and the Balrog doing a demonic attack and also a speed attack.

Don't forget you need to #include and #include

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 5 derived classes. (There will be one such cout statement to delete in each of the 5 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. (Don't 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 (i.e., return it to it's previous state).

Make getDamage() a virtual function. This will be important so that in your "battleArena" function (see below) you can say "Creature1.getDamage()" and the damage will automatically be calculated for the correct Creature. Note that the parameters for "battleArena" will be of type "Creature" and they will need to be pass-by-reference. (You might try making them pass-by-value to see what happens.)

Create a new client program (discard the client program from part 1 of the assignment). Make a function in your client program that is called from your main function, battleArena(Creature &Creature1, Creature& Creature2), that takes two Creature objects as parameters. The function should calculate the damage done by Creature1, subtract that amount from Creature2's hitpoints, and vice versa. (When I say "subtract that amount from Creature2's hitpoints, I mean that the actual hitpoints data member of the Creature2 object will be modified. Also note that this means that both attacks are happening simultaneously; that is, if Creature2 dies because of Creature1's attack, Creature2 still gets a chance to attack back.) If both Creatures end up with 0 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 battle is over. The function should loop until either a tie or over. Since the getDamage() function is virtual it should invoke the getDamage() function defined for the appropriate Creature. Test your program with several battles involving different Creatures. I've provided a sample main function below. Your only remaining task is to write the "battleArena" function and expand the main function so that the "battleArena" function is tested with a variety of different Creatures.

All of the classes should still be in the cs_creature namespace

Assignment 7 Reflection [0 points]

You won't submit anything or be graded for this but I think it is an important part of the assignment so I made it a separate part.

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 3 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.

Submit Your Work

Name your client program a7.cpp. Name each class file according to the class it contains (for example, elf.h, creature.cpp). There will be 13 files. Execute your client program and copy/paste the output into the bottom of your client file, making it into a comment. Zip the files and use the Assignment Submission link to submit the zip file. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program works as required.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!