Reboot function implementation

Currently, I do not understand how to implement a function for my Weapon class to reload.

What my program should do (for training purposes) has the Weapon abstract class, which has two classes stemming from it: Sword and Crossbow. The Sword class works as intended, and therefore needs to play. However, the Crossbow class must determine if it is loaded or not, and if it is not loaded, it will load it and run it.

For example, here is a demo output that my teacher gave the class: (this is how it should look)

The crossbow deals 15 damage.

The sword deals 10 damage.

The crossbow is not loaded!

The crossbow deals 15 damage.

Optimistic, I would like it to look similar.

Please, no direct answers. This is homework, and I would really like to know. I am looking for points in the right direction.

Weapon.h

#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; } 
+4
source share
2 answers

Do not try to add field ammunition to your base class. The sword has no concept of ammunition. Add it to your Crossbow class. This is the bool flag as a flag.

You can assume that it is not loaded when creating Crossbow, so in the initial list of the constructor you just add the loaded one (false)

Then you should change your β€œattack” by checking the download first. If not loaded, first reboot the reboot function view:

 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 this, your attack and reboot cannot be const, because the function has a value.

Now you can either call reload () to simply reboot, or simply call attack () whether it is loaded or not.

+2
source

Not sure if it suits you, but try adding a field to Weapon called something like ammo . Then the designers set it to the required amount, and the Attack crossbow must check whether there is ammunition in the quiver or not. If not, set the ammo value to its original value. It is very simple. For Sword just don't check the ammo! :)

Also note that this is bad practice and only a temporary fix until you understand these concepts. The right way is to create a new class that inherits from Weapon , and this class will be inherited by a weapon with ammunition.

Hope I helped and enjoy learning C ++! :)

+2
source

Source: https://habr.com/ru/post/1500964/


All Articles