Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected: HeroType type; string name; double health; double attackStrength; public:

Base Class

enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected: HeroType type; string name; double health; double attackStrength; public: Character(HeroType type, const string &name, double health, double attackStrength); HeroType getType() const; const string & getName() const; /* Returns the whole number of the health value (static_cast to int). */ int getHealth() const; /* Reduces health value by amount passed in. */ void damage(double d); /* Returns true if getHealth() returns an integer greater than 0, otherwise false */ bool isAlive() const; virtual void attack(Character &) = 0; }; 

Derived Classes

Warrior

Stores the warrior's allegiance as a string.

The warrior does not attack warriors that have the same allegiance.

The damage done by the warrior is the percentage of the warrior's health remaining (health / MAX_HEALTH) multiplied by the warrior's attack strength.

Elf

Stores the elf's family name as a string.

The elf does not attack an elf from its own family.

The damage done by the elf is the percentage of the elf's health remaining (health / MAX_HEALTH) multiplied by the elf's attack strength.

Wizard

Stores the wizard's rank as an int.

When a wizard attacks another wizard, the damage done is the wizard's attack strength multiplied by the ratio of the attacking wizard's rank over the defending wizard's rank.

The damage done to non-wizards is just the attack strength. The wizard's health is not taken into consideration.

Dynamic casting type of Character in attack function

In order to access the Warrior data field allegiance using the Character reference passed in to the attack function, you will need to dynamic cast the Character reference to a Warrior reference.

Here's an example of dynamic casting a Character reference named opponent to a Warrior reference named opp:

Warrior &opp = dynamic_cast(opponent); 

You will need to do the same for the Wizard and Elf attack functions, only dynamic casting to Wizard or Elf reference instead.

HeroType

Notice the enum declaration above the Character class declaration. This creates a special type called HeroType that has the values, WARRIOR, ELF, and WIZARD. Those are the values you store in a variable of type HeroType. For example, you can initialize a variable of type HeroType and set it to the value of WARRIOR like this:

HeroType type = WARRIOR; 

You can compare a variable named t of type HeroType to one of the HeroType values like this:

if (t == WARRIOR) { // do something based on t being a warrior } 

Example main function

This shows you what your attack function should do in all situations. Look below this to find the main function you will need to pass the zyBook tests.

#include using namespace std; #include "Warrior.h" #include "Elf.h" #include "Wizard.h" int main() { Warrior w1("Arthur", 100, 5, "King George"); Warrior w2("Jane", 100, 6, "King George"); Warrior w3("Bob", 100, 4, "Queen Emily"); Elf e1("Raegron", 100, 4, "Sylvarian"); Elf e2("Cereasstar", 100, 3, "Sylvarian"); Elf e3("Melimion", 100, 4, "Valinorian"); Wizard wz1("Merlin", 100, 5, 10); Wizard wz2("Adali", 100, 5, 8); Wizard wz3("Vrydore", 100, 4, 6); e1.attack(w1); cout  

The output of this main function will look like this:

Elf Raegron shoots an arrow at Arthur --- TWANG!! Arthur takes 4 damage. Elf Raegron does not attack Elf Cereasstar. They are both members of the Sylvarian family. Warrior Jane does not attack Warrior Arthur. They share an allegiance with King George. Warrior Bob attacks Arthur --- SLASH!! Arthur takes 4 damage. Wizard Merlin attacks Adali --- POOF!! Adali takes 6.25 damage. Wizard Merlin attacks Vrydore --- POOF!! Vrydore takes 8.33333 damage.

main.cpp

#include #include #include

using namespace std;

#include "Character.h" #include "Warrior.h" #include "Elf.h" #include "Wizard.h"

int main() { int seed; cout > seed; cout

vector adventurers; adventurers.push_back(new Warrior("Arthur", 100, 5, "King George")); adventurers.push_back(new Warrior("Jane", 100, 6, "King George")); adventurers.push_back(new Warrior("Bob", 100, 4, "Queen Emily")); adventurers.push_back(new Elf("Raegron", 100, 4, "Sylvarian")); adventurers.push_back(new Elf("Cereasstar", 100, 3, "Sylvarian")); adventurers.push_back(new Elf("Melimion", 100, 4, "Valinorian")); adventurers.push_back(new Wizard("Merlin", 100, 5, 10)); adventurers.push_back(new Wizard("Adali", 100, 5, 8)); adventurers.push_back(new Wizard("Vrydore", 100, 4, 6));

unsigned numAttacks = 10 + rand() % 11; unsigned attacker, defender; for (unsigned i = 0; i attack(*adventurers.at(defender)); cout getName() getHealth()

return 0; }

Character.h

#include

using namespace std;

#ifndef __CHARACTER_H__ #define __CHARACTER_H__

enum HeroType {WARRIOR, ELF, WIZARD};

const double MAX_HEALTH = 100.0;

class Character { protected: HeroType type; string name; double health; double attackStrength;

public: Character(HeroType, const string &, double, double); HeroType getType() const; const string & getName() const; int getHealth() const; void damage(double d); bool isAlive() const; virtual void attack(Character &) = 0; };

#endif

image text in transcribedIDE: zybook C++

EzyBooks catalo LAB ACTIVITY 8.11.1: LAB 8: Polymorphism 0/35 Submission Instructions Downloadable files Character.h main.cpp Download Compile command g++ main.cpp Warrior.cpp Character.cpp Wizard.cpp Elf.cpp -Wall -Werror -Wuninitialized -o a.out We will use this command to compile your code Upload your files below by dragging and dropping into the area or choosing a file on your hard drive. Warrior.cpp Drag file here or Choose on hard drive. Character.cpp Drag file here or Choose on hard drive. Wizard.cpp Drag file here or Choose on hard drive Warrior.h Elf.h Wizard.h Drag file here or Choose on hard drive. Drag file here or Choose on hard drive. Drag file here or Choose on hard drive. Elf.cpp Drag file here or Choose on hard drive

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions