Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and code a class named Hero that holds information about a fictional hero character. Place your class definition in a header file named Hero.h

Design and code a class named Hero that holds information about a fictional hero character. Place your class definition in a header file named Hero.h and your function definitions in an implementation file named Hero.cpp. Include in your design all of the statements necessary to compile and to run your code successfully under a standard C++ compiler.

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;

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.

Hero.cpp

#include
#include
#include "Hero.h"
using namespace std;
//////////////////////////////////////////////
// Default constructor
//
Hero::Hero ()
{
}
///////////////////////////////////////////////////
// Constructor
//
Hero::Hero (const char name[], unsigned maximumHealth, unsigned attack)
{
}
/////////////////////////////////////////////////////////
//
// Hero::display function
void Hero::display (ostream & out) const
{
}
/////////////////////////////////////////////////
// Hero::isEmpty ()
// return true if the Hero object is uninitialized
//
bool Hero::isEmpty () const
{
}
/////////////////////////////////////////////////
// sets the Hero object's health back to full
//
void Hero::respawn()
{

}

Hero.h

#ifndef HERO_H
#define HERO_H
#include
class Hero
{
char m_name[41];
unsigned m_attack;
unsigned m_maximumHealth;
int m_health;
bool isEmpty () const;
public:
// constructors
Hero ();
Hero (const char name[], unsigned maximumHealth, unsigned attack);
// member functions
void respawn();
bool isAlive () const { return m_health > 0; }
unsigned getAttack () const { return m_attack; }
// display function
void display (std::ostream &) const;
// friend global helper function to assign damage to 2 heroes at the same time.
friend void apply_damage ( Hero& A, Hero& B);
};

#endif

Fight.h

#ifndef FIGHT_H
#define FIGHT_H
const Hero & operator* (const Hero & a, const Hero & b);
void apply_damage (Hero& A, Hero& B);

#endif

Fight.cpp

#include
#include "Hero.h"
#include "fight.h"
using namespace std;
//////////////////////
// Global helper function
// compute the damage that A inflicts on B
// and of B on A
//
void apply_damage (Hero& A, Hero& B)
{
}
//////////////////////////////////////////////////////////////////
// Global helper operator
// rather than type in fight(hercules, theseus), we use an operator
// so you can type in hercules * theseus
//
// returns a reference to the winner object
//
// so that if hercules is stronger,
// (hercules * theseus) == hercules
// will be true.
//
// note the inputs are const, so that you can be sure that the heros will be unharmed during the fight.
//
const Hero & operator* (const Hero & first, const Hero & second)
{
// Display the names of the people fighting
cout << "AncientBattle! ";
first.display (cout);
cout << " vs ";
second.display (cout);
cout << " : ";
// We want our heroes to exit the battle unharmed, so
// we make the input arguments const.
// So how can we modify the objects during the fight?
// We make copies of them.
Hero A = first;
Hero B = second;
const Hero *winner = nullptr;
// Now A will fight B, and *winner will point to the winner.
// Main fight loop
unsigned int rounds = 0;
// loop while both are still alive
// fight for 100 rounds
while (A.isAlive () && B.isAlive () && rounds < 100)
{
rounds++;
apply_damage(A, B);
}
// if we got here, then one Hero is dead, or if both are alive then it was a draw.
bool draw;
if (A.isAlive() && B.isAlive()) { draw = true; }
else { draw = false; }
// if it was a draw, then we decide by tossing an unfair coin and always
// declare that A was the winner.
if (draw)
{ winner = &first; }
else if (A.isAlive ())
{ winner = &first; }
else
{ winner = &second; }
// print it out
cout << "Winner is ";
winner->display(cout);
cout << " in " << rounds << " rounds." << endl;
return *winner;

}

}

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 3 Lnai 9853

Authors: Bettina Berendt ,Bjorn Bringmann ,Elisa Fromont ,Gemma Garriga ,Pauli Miettinen ,Nikolaj Tatti ,Volker Tresp

1st Edition

3319461303, 978-3319461304

More Books

Students also viewed these Databases questions