Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Heros have a simple model to determine who wins in a fight. Each hero has an integer health number. If the health is 0, the

Heros have a simple model to determine who wins in a fight. Each hero has an integer health number. If the health is 0, the Hero is not alive. The health can go up to a maximum as stored in the m_maximumHealth variable.

During a fight a Hero can attack another Hero. Each Hero has their own specific attack strength. The attack inflicts damage on the receiver. In a fight where Hero 1 attacks Hero 2, and Hero 2 takes damage.

Damage (for hero 2) = - Attack (for hero 1)

Damage is subtracted from the health. The winner is whoever is alive when the opponent is dead.

The Hero class must contain the following data

Member variable name

Data type

Default value

Description

m_name[41]

char

String to hold the heros name.

Maximum length 40 chars + \0 terminator.

m_health

int

0

The heros health, from 0 to the value specified in m_maximumHealth

m_maximumHealth

unsigned

0

The maximum health points of the Hero.

m_attack

unsigned

0

The attack strength of the hero.

Upon instantiation, a Hero object may receive no information, or may receive three values:

A string which can be nullptr or empty, giving the name

The Maximum Health value

The attack value

The constructor should initialize the Health to maximumHealth.

Your design must also include the following member functions and one helper operator:

Function Declaration

bool isEmpty() const

Returns true if the object is in the safe empty state;

otherwise.

unsigned getAttack() const

Returns the attack strength for the hero.

The return value can be 0 for an object in the safe empty state.

Note: this means you do not have to explicitly check for it!

void display (std::ostream&) const

prints the heros name to the output stream.

If the current object is empty, this function should print nothing.

bool isAlive() const

Returns true if the Hero is alive, false if not.

A hero is alive if his/her health is > 0.

void respawn()

Restore the Heros health to his/her maximum value.

Global helper functions

Implement the following helper function in the file fight.cpp.

friend void apply_damage(Hero& A, Hero& B)

A function to inflict 1 unit of damage on each Hero.

It should deduct from the Heros health, according to the damage formula given previously. For example, if Hero A and B attack each other,

A.m_health -= B.m_attack;

B.m_health -= A.m_attack;

Also put appropriate checks for 0 values in the function. Note that the Heros health should not decrease to below 0.

A function in fight.cpp has been given to handle a complete fight between two heroes, A and B. It is implemented as the * operator. Note the following features of the function.

The inputs are const Hero&. This ensures that the heroes are unharmed during the tournament. The first thing the function does is make a copy of them to local variables. Note that you do not need a copy constructor...because the class does not contain any dynamically allocated resources.

It returns a reference to the winner. This allows you to pass the return value of the function to another function call.

The following program uses your Hero class and produces the output shown below:

#include

#include "Hero.h"

#include "fight.h"

using namespace std;

int main ()

cout << endl << "Greek Heros";

Hero hercules("Hercules",32, 4);

Hero theseus("Theseus",14, 5);

Hero oddyseus("Odysseus",15, 3);

Hero ajax("Ajax",17, 5);

Hero achilles("Achilles",20, 6);

Hero hector("Hector",30, 5);

Hero atalanta("Atalanta",10, 3);

Hero hippolyta("Hippolyta", 10, 2);

cout << endl << "Quarter Finals" << endl;

const Hero& greek_winner1 = achilles * hector;

const Hero& greek_winner2 = hercules * theseus;

const Hero& greek_winner3 = oddyseus * ajax;

const Hero& greek_winner4 = atalanta* hippolyta;

cout << endl << "Semi Finals" << endl;

const Hero& greek_winner_semifinal1 = greek_winner1* greek_winner2;

const Hero& greek_winner_semifinal2 = greek_winner3* greek_winner4;

cout << endl << "Finals" << endl;

greek_winner_semifinal1 * greek_winner_semifinal2;

OUTPUT:

Greek Heros

Quarter Finals

Battle! Achilles vs Hector : Winner is Hector in 4 rounds.

Battle! Hercules vs Theseus : Winner is Hercules in 4 rounds.

Battle! Odysseus vs Ajax : Winner is Ajax in 3 rounds.

Battle! Atalanta vs Hippolyta : Winner is Atalanta in 4 rounds.

Semi Finals

Battle! Hector vs Hercules : Winner is Hector in 7 rounds.

Battle! Ajax vs Atalanta : Winner is Ajax in 2 rounds.

Finals

Battle! Hector vs Ajax : Winner is Hector in 4 rounds.

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

More Books

Students also viewed these Databases questions