Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**NEED ONLY SUPERHERO.H & SUPERHERO.CPP and need to update Hero.h TO GET THE FOLLOWING OUTPUT Derive a class named SuperHero from the Hero class that

**NEED ONLY SUPERHERO.H & SUPERHERO.CPP and need to update Hero.h TO GET THE FOLLOWING OUTPUT

Derive a class named SuperHero from the Hero class that you designed in the section. Include in your design all of the statements and keywords necessary to compile and to run your code successfully under a standard C++ compiler.

SuperHeros behave the same as Heros, except that they have SuperPowers! A SuperHero still has a regular normal attack, and health...but he/she can also use his SuperPower to attack, and to defendbut only against other superheros. When a SuperHero fights against a Hero, he/she does not use his SuperPowers. They only uses their regular Hero attack.

Upon instantiation, a SuperHero object may receive no information, or it may receive five values:

The name, maximum health and attack values similar to the Hero class.

An unsigned integer for the SuperPowerAttack strength of the SuperHero.

A unsigned integer representing a The SuperPowerDefend strength for the SuperHero.

Fighting for the SuperHero is similar to the Hero class. Damage is calculated differently though. When Hero1 attacks Hero2

Build constructors to handle these instantiation types. Invalid input sets the object in the safe, empty state.

SuperHero should add the following new member variables.

m_superPowerAttack

unsigned

0

The SuperPower attacking strength

m_superPowerDefend

unsigned

0

The SuperPower defense strength

SuperHero also adds the following member functions: Note that they have the same names as the functions in Hero. They are supposed to shadow the functions in the Hero class.

unsigned getAttack() const -- This method should return the SuperPower Attack of the superhero instead of the regular Attack value.

Note that this method has the same name and prototype as the one in Hero.

You will also need a new global helper function, apply_damage. It will compute the damager both SuperHeros inflicts on each other. This should have the following prototype

friend void apply_damage ( SuperHero& A, SuperHero& B);

The following code uses your Hero and SuperHero classes and produces the output shown under it:

int main ()

cout << endl;

line(60); // print some dashes

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;

const Hero& greek_final = greek_winner_semifinal1 * greek_winner_semifinal2;

cout << endl << "Comic book SuperHeros";

SuperHero superman("Superman",50, 9, 10, 9) ;

SuperHero hulk("The_Hulk",70, 6, 20, 3) ;

SuperHero wonderwoman ("WonderWoman",80, 5, 15, 10) ;

SuperHero raven("Raven",30, 10, 12, 5) ;

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

const SuperHero& comic_winner1 = superman * hulk;

const SuperHero& comic_winner2 = wonderwoman * raven;

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

const SuperHero& comic_final = comic_winner1 * comic_winner2;

cout << endl << "Best Greeks Hero vs Best Comic Book SuperHero" << endl;

greek_final * comic_final;

------------------------------------------------------------

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.

------------------------------------------------------------

Comic book SuperHeros

Semi Finals

Fight! Superman vs The_Hulk : Winner is The_Hulk in 5 rounds.

Fight! WonderWoman vs Raven : Winner is WonderWoman in 3 rounds.

Finals

Fight! The_Hulk vs WonderWoman : Winner is WonderWoman in 6 rounds.

------------------------------------------------------------

Best Greeks Hero vs Best Comic Book SuperHero

Battle! Hector vs WonderWoman : Winner is WonderWoman in 6 rounds.

Hero.h

#ifndef HERO_H #define HERO_H

#include

class Hero { char m_name[41]; unsigned m_attack; unsigned m_maximumHealth;

bool isEmpty() const; protected : int m_health;

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

Hero.cpp

#include #include #include "Hero.h"

using namespace std;

////////////////////////////////////////////// // Default constructor // Hero::Hero () { m_name[0] = 0; m_health = 0 ; m_attack = 0 ; m_maximumHealth = 0 ;

}

Hero::Hero (const char name[], unsigned maximumHealth, unsigned attack) {

strcpy(m_name , name); m_maximumHealth = maximumHealth ;

m_health = maximumHealth; m_attack = attack ; }

///////////////////////////////////////////////////////// // // Hero::display function void Hero::display(ostream & out) const {

if (isEmpty()) { return;} else { out << m_name; }

}

///////////////////////////////////////////////// // Hero::isEmpty () // return true if the Hero object is uninitialized // bool Hero::isEmpty() const { if (m_name[0] == 0 && m_attack == 0 &&

m_maximumHealth == 0 && m_health == 0)

{ return true;

} else {

return false; }

}

///////////////////////////////////////////////// // sets the Hero object's health back to full // void Hero::respawn() { }

SuperHero.h

#ifndef SUPERHERO_H

#define SUPERHERO_H

#include "Hero.h"

class SuperHero: public Hero

{

unsigned m_superPowerAttack;

unsigned m_superPowerDefend;

public:

// constructors

SuperHero();

SuperHero(const char* name,

unsigned health,

unsigned attack,

// the following 2 parameters are special for SuperHeros

unsigned superPowerAttack,

unsigned superPowerDefend);

unsigned getAttack() const;

// friend global helper function to assign damage to 2 heroes at the same time.

friend void apply_damage ( SuperHero& A, SuperHero& B);

};

#endif

SuperHero.cpp

#include

#include "SuperHero.h"

SuperHero::SuperHero() : Hero()

{

}

SuperHero::SuperHero( const char* name, unsigned maximumHealth, unsigned attack,

unsigned superPowerAttack, unsigned superPowerDefend

) : Hero(name, maximumHealth, attack)

{

}

unsigned SuperHero::getAttack() const

{

}

Fight.h

#ifndef FIGHT_H

#define FIGHT_H

const SuperHero & operator* (const SuperHero & a, const SuperHero & b);

const Hero & operator* (const Hero & a, const Hero & b);

void apply_damage (SuperHero& A, SuperHero& 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)

{

B.m_health = B.m_health - A.m_attack;

A.m_health = A.m_health - B.m_attack;

if (A.m_health <= 0) {

A.m_health = 0; }

if ( B.m_health <= 0)

{ B.m_health = 0; }

}

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

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

IBM Db2 11 1 Certification Guide Explore Techniques To Master Database Programming And Administration Tasks In IBM Db2

Authors: Mohankumar Saraswatipura ,Robert Collins

1st Edition

1788626915, 978-1788626910

More Books

Students also viewed these Databases questions