So i've been playing with the code for a while now and doing some researcha nd I found this method works, but it seems tedious to do and fix, is there a simpler way?
Code:
#include <iostream>
class Entity
{
public:
Entity(std::string name, int health, int damage) : m_name(name), m_health(health), m_damage(damage){}
virtual ~Entity() {}
protected:
std::string m_name = {};
int m_health = {};
int m_damage = {};
};
class Weapon : public Entity
{
public:
Weapon(std::string name, int health, int damage)
: Entity(name, health, damage){}
virtual ~Weapon() {}
virtual void Fire() = 0;
virtual void Reload()
{
std::cout << "Reloaded weapon\n";
}
protected:
int m_magazineMaxSize = {};
};
class Handgun : public Weapon
{
public:
Handgun(std::string name, int health, int damage)
: Weapon(name, health, damage){}
void Fire() override
{
std::cout << "9mm " << m_name << "!\n";
std::cout << "Health is: " << m_health << "\n";
std::cout << "Damage this weapon does: " << m_damage << "\n";
}
void Reload() override
{
std::cout << m_name << " reloaded!\n";
}
};
class Shotgun : public Weapon
{
public:
Shotgun(std::string name, int health, int damage) : Weapon(name, health, damage){}
void Fire() override
{
std::cout << "12 Guage " << m_name << "\n";
std::cout << "Health is: " << m_health << "\n";
std::cout << "Damage this weapon does: " << m_damage << "\n";
}
void Reload() override
{
std::cout << m_name << " reloaded!\n";
}
};
void DischargeWeapon(Weapon& weapon)
{
weapon.Fire();
}
void ReloadWeapon(Weapon& weapon)
{
weapon.Reload();
}
int main()
{
Handgun handgun("Handgun", 5, 10);
Shotgun shotgun("Shotgun", 10, 30);
DischargeWeapon(handgun);
ReloadWeapon(handgun);
std::cout << "\n";
DischargeWeapon(shotgun);
ReloadWeapon(shotgun);
}