Trying to create health and damage gauges for my primitive game

I need to Create a getHealth member function at the Creature level that returns the value of m_Health. Create a wound member function at the Creature level that decrements the value of m_Health by the amount specified by the integer input parameter.


//Abstract Creature
//Demonstrates abstract classes

#include <iostream>
using namespace std;

class Creature //abstract class
{
public:
Creature(int health = 100);
virtual void Greet() const = 0; //pure virtual member function
virtual void DisplayHealth() const;

protected:
int m_Health;
};

Creature::Creature(int health):
m_Health(health)
{}

Creature::DisplayHealth() const
{
cout << "Health: " << m_Health << endl;
}

class Orc : public Creature
{
public:
Orc(int health = 120);
virtual void Greet() const;
};

Orc::Orc(int health):
Creature(health)
{}

Orc::Greet() const
{
cout << "The orc grunts hello.\n";
}

int main()
{
Creature* pCreature = new Orc();
pCreature->Greet();
pCreature->DisplayHealth();

return 0;
}

const int NUM_CREATURES = 3;


Creature* creature[NUM_CREATURES];


Creature* creaturePtr = new Orc();

creature[0] = creaturePtr;


creature[1] = new Orc();

creature[2] = new OrcBoss();


creaturePtr = creature[0];


creaturePtr->wound(60);

creaturePtr->wound(80);


creature[1]->wound(90);


creature[2]->wound(100);


// For all creatures...

for (int i = 0; i < NUM_CREATURES; i++)

Creature::~Creature()

{

// Be sure this creature is still alive!

if (creature[i] != NULL)

{

// Let's see the creature display output.

creature[i]->greet();

creature[i]->DisplayHealth();


// If this creature's health value is zero or less, let's kill him.

if (creature[i]->getHealth() <= 180) {

delete creature[i];

creature[i] = NULL;

}

cout << endl;

}

}


return 0;





//Overriding Boss
//Demonstrates calling and overriding base member functions

#include <iostream>

using namespace std;

class Enemy
{
public:
Enemy(int damage = 10);
void virtual Taunt() const; //made virtual to be overridden
void virtual Attack() const; //made virtual to be overridden

private:
int m_Damage;
};

Enemy::Enemy(int damage):
m_Damage(damage)
{}

Enemy::Taunt() const
{
cout << "The enemy says he will fight you.\n";
}

Enemy::Attack() const
{
cout << "Attack! Inflicts " << m_Damage << " damage points.";
}

class Boss : public Enemy
{
public:
Boss(int damage = 30);
void virtual Taunt() const; //optional use of keyword virtual
void virtual Attack() const; //optional use of keyword virtual
};

Boss::Boss(int damage):
Enemy(damage) //call base class constructor with argument
{}

Boss::Taunt() const //override base class member function
{
cout << "The boss says he will end your pitiful existence.\n";
}

Boss::Attack() const //override base class member function
{
Enemy::Attack(); //call base class member function
cout << " And laughs heartily at you.\n";
}

main()
{
cout << "Enemy object:\n";
Enemy anEnemy;
anEnemy.Taunt();
anEnemy.Attack();

cout << "\n\nBoss object:\n";
Boss aBoss;
aBoss.Taunt();
aBoss.Attack();

return 0;
}

{
cout << "OrcBosses never truly die!" << endl;


Topic archived. No new replies allowed.