Using inheritance, this program will allow the creation of different types of soldiers based on the Soldier base class. Each type of soldier should handle marching and fireing differently.
For example:
A scout would have less ammo, and would use up less stamina to march
A machine gunner would have more ammo, but go through it quicker
A flamethrower would use up more stamina by marching
I'm having trouble trying to call the different classes... I want to be able to say if the user selects scout then less ammo would show up and less stamina would be taken while marching.. instead of the base soldiers ammo and marching.. how and where should i call this
Code:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
void Seed()
{
srand(time(0));
}
int GetRandNum(int max)
{
return rand() % max + 1;
}
class Soldier
{
public:
Soldier(string input);
int March();
int Fire();
void Rest();
void LoseStamina(int distance);
void Reload();
void LoseAmmo(int shots);
void DisplayStats();
protected:
string m_name;
int m_ammo;
int m_maxAmmo;
int m_stamina;
int m_maxStamina;
};
Soldier::Soldier(string input)
{
m_name = input;
m_stamina = 50;
m_maxStamina = m_stamina;
m_ammo = 20;
m_maxAmmo = m_ammo;
}
int Soldier::March()
{
return GetRandNum(10);
}
int Soldier::Fire()
{
return GetRandNum(4);
}
void Soldier::Rest()
{
m_stamina = m_maxStamina;
}
void Soldier::Reload()
{
m_ammo = m_maxAmmo;
}
void Soldier::LoseStamina(int distance)
{
m_stamina -= distance;
cout << m_name << " you marched " << distance << " miles, you lose " << distance << " stamina " << endl;
if(m_stamina <= 0)
cout << m_name << " can't move " << endl;
}
void Soldier::LoseAmmo(int shots)
{
m_ammo -= shots;
cout << m_name << " you shot " << shots << " bullets " << endl;
if (m_ammo <=0)
cout << m_name << " is out of ammo " << endl;
}
void Soldier::DisplayStats()
{
cout << "Name: " << m_name << endl;
cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
}
class Scout : public Soldier
{
public:
Scout(string input);
void LoseStamina(int distance);
private:
int m_stamina;
int m_maxStamina;
int m_ammo;
int m_maxAmmo;
};
Scout::Scout(string input) : Soldier(input)
{
m_name = input;
m_stamina = 100;
m_maxStamina = m_stamina;
m_ammo = 5;
m_maxAmmo = m_ammo;
}
void Scout::LoseStamina(int distance)
{
m_stamina -= distance/2;
cout << m_name << ", you lost " << distance << " stamina " << endl;
if(m_stamina <= 0)
cout << m_name << " can't move " << endl;
}
class Machiner : public Soldier
{
public:
Machiner(string input);
void LoseStamina(int distance);
void LoseAmmo(int shots);
private:
int m_stamina;
int m_maxStamina;
int m_ammo;
int m_maxAmmo;
};
Machiner::Machiner(string input) : Soldier(input)
{
m_stamina = 25;
m_maxStamina = m_stamina;
m_ammo = 100;
m_maxAmmo = m_ammo;
}
void Machiner::LoseStamina(int distance)
{
m_stamina -= distance * 2;
cout << m_name << ", you lost " << distance << " stamina " << endl;
if(m_stamina <= 0)
cout << m_name << " can't move " << endl;
}
void Machiner::LoseAmmo(int shots)
{
m_ammo -= shots * 4;
cout << m_name << " you shot " << shots << " bullets " << endl;
if (m_ammo <=0)
cout << m_name << " is out of ammo " << endl;
}
class Sniper : public Soldier
{
public:
Sniper(string input);
void LoseAmmo(int shots);
private:
};
Sniper::Sniper(string input) : Soldier(input)
{
};
void Sniper::LoseAmmo(int shots)
{
m_ammo - 1;
cout << m_name << " you shot " << shots << " bullets " << endl;
if (m_ammo <=0)
cout << m_name << " is out of ammo " << endl;
}
int main()
{
string name, type;
int input, stamina, ammo;
bool play = true;
Seed();
cout << "Welcome to training soldier" << endl;
cout << "Please enter your name" << endl;
cin >> name;
Soldier player(name);
system("CLS");
cout << "Greetings Private " << name << "!" << endl;
cout << "Are you a Scout, Machiner, or Sniper Soldier? " << endl;
cin >> type;
{
if (type == "Scout")
{
cout << "You are a scout!" << endl;
Scout player(name);
if (type == "Machiner")
{
cout << "You are a machiner!" << endl;
if (type == "Sniper")
{
cout << "You are a sniper!" << endl;
}
}
}
}
while(play)
{
cout << "What would you like to do?" << endl;
cout << "Press 1 to do marching drills" <<endl;
cout << "Press 2 to shoot your weapon" << endl;
cout << "Press 3 to rest a bit." << endl;
cout << "Press 4 to reload your weapon." << endl;
cout << "Press 5 to view your stats." << endl;
cin >> input;
system("CLS");
switch(input)
{
case 1:
{
if(type == "Scout")
{
Scout.LoseStamina(player.March());
}
player.LoseStamina(player.March());
}
system("PAUSE");
break;
case 2:
{
player.LoseAmmo(player.Fire());
}
system("PAUSE");
break;
case 3:
{
player.Rest();
cout << "Your Stamina has been regained" << endl;
cout << "Get back to marching soldier!" << endl;
}
system("PAUSE");
break;
case 4:
{
player.Reload();
cout << "Your gun is now reloaded" << endl;
cout << "Try hitting the target this time!" << endl;
}
system("PAUSE");
break;
case 5:
{
player.DisplayStats();
}
system("PAUSE");
break;
}
}
system("PAUSE");
return 0;
}