Reputation: 548
I'm currently at a loss as to how to implement a function to my Weapon class for reloading.
What my program is supposed to do (for learning purposes) is have an abstract Weapon class that has two classes deriving from it, Sword and Crossbow. The Sword class works as its supposed to, and therefore needs to toying with. However, the Crossbow class needs to detect whether its loaded or not, and if it is not loaded it will then load it and fire it.
For an example, here is the demo output my teacher gave to the class: (this is how it is supposed to look)
Crossbow inflicts 15 damage points.
Sword inflicts 10 damage points.
Crossbow not loaded!
Crossbow inflicts 15 damage points.
Optimistically speaking, I'd like to have it look similar.
Please, no direct answers. This is homework and I'd really like to learn this. I'm looking for points in the right direction.
#include <iostream>
using namespace std;
class Weapon
{
public:
Weapon(int damage = 0);
virtual void Attack() const = 0;
protected:
int m_Damage;
};
Weapon::Weapon(int damage) : m_Damage(damage)
{}
class Sword : public Weapon
{
public:
Sword(int damage = 10);
virtual void Attack() const;
};
Sword::Sword(int damage): Weapon(damage)
{}
void Sword::Attack() const
{
cout << "The sword hits for " << m_Damage << " points of damage" << endl;
}
class Crossbow : public Weapon
{
public:
Crossbow(int damage = 20);
virtual void Attack() const;
void Reload() const;
};
Crossbow::Crossbow(int damage) : Weapon(damage)
{}
void Crossbow::Attack() const
{
cout << "The crossbow hits for " << m_Damage << " points of damage" << endl;
}
void Crossbow::Reload() const
{
cout << "Crossbow not loaded! Please reload" << endl;
}
Upvotes: 4
Views: 2090
Reputation: 1866
Not sure if it suits you, but try to add a field to Weapon
called something like ammo
. The constructors will then set it to the required amount, and the crossbow's Attack
should have to check if there is any ammo left in the quiver or not. If not, set the value of ammo
to the original. This is really simple. For the Sword
, simply, don't check the ammo! :)
Also note, that it is a bad practice and only a temporary fix until you better understand these concepts. The correct way would be creating a new class that inherits from Weapon
and this class will be inherited by the weapons that have ammo.
Hope I helped, and have fun learning C++ ! :)
Upvotes: 2
Reputation: 1028
Don't try to add field "ammo" in your Base class. Sword does not have the concept "ammo". Add it in your Crossbow class. It can be a bool type as flag.
You can assume it's not loaded while creating a Crossbow, so in the constructor initial list, you just add loaded(false)
Then you should change your "attack" by checking if loaded first. If not loaded, reload first kind of reload function:
void Crossbow::reload() {
if ( !loaded ) {
cout << "Crossbow loading ..." << endl;
loaded = true;
}
cout << "Crossbow reloaded" << endl;
}
void Crossbow::attack() {
if ( !loaded ) {
cout << "Crossbow not loaded! Please reload" << endl;
reload();
}
cout << "The crossbow hits for " << m_Damage << " points of damage" << endl;
loaded = false;
}
For doing this, your attack and reload cannot be const because there is a value changed in the function.
Now you can either call reload() for just reloading or just call attack() regardless of whether it's loaded or not.
Upvotes: 2