#include <iostream>
#include <string>
usingnamespace std;
class plasmaPistolClass{
private:
int ammo; //an integer value indicating the number of plasma bolts left in the plasma pistol
int rateOfFire; //an integer value indicating the number of bolts fired with one trigger pull.
//The max value is 10 the minimum value is 1.
//Ensure that the minimum and max values are not exceeded.
int destructivePower; //an integer from 1 to 10 indicating the destructive effect of the weapon.
//Ensure that the minimum and max values are not exceeded.
public:
bool safetyOn; //a boolean value indicating if the pistols safety feature is activated
int maxAmmo; //an integer value indicating the maximum number of ammo bolts the plasma pistol can hold
//The class should also include these public methods:
void pressTrigger(void); //fires the plasma pistol and decreases the ammo count based on the rateOfFire property.
//The pistol should not fire if the safetyOn property is true.
void load(int nmbrOfBolts); //adds ammo bolts to the plasma pistol.
//The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property
void setDestructivePower(int powerSetting); //changes the destructive power of the plasma pistol
int showDestructivePower(void); //returns the value of the destructivePower property.
//Ensures that the max and minimum values are not exceeded.
void setRateOfFire(int boltsPerTriggerPress); //changes the number of plasma bolts fired by the plasma pistol.
//Ensures that the max and minimum values are not exceeded.
int showRateOfFire(void); //returns the value of the rateOfFire property
int ammoRemaining(void); //displays the amount of ammo remaining in the pistol
//The plasma pistol class should have two constructors:
//A default constructor that assigns default values to both the private and public properties of the object.
plasmaPistolClass();
//An overload constructor that allows the destructive power and maxAmmo properties to be set when the object is instantiated.
plasmaPistolClass(int, int);
};
int main(void)
{
//test the plasma pistol class
//create 2 pistols
plasmaPistolClass pistolLeft;
plasmaPistolClass pistolRight(5,30);
//ready a pistol
cout << "Firing Right Pistol!" << endl << endl;
//fire a pistol
pistolRight.pressTrigger();
cout << endl;
//Instantiate two plasma pistols. Use both of the constructors.
//Set the following properties of one pistol: rate of fire, max ammo, destructive power and ammo.
//Display the value of destructive power, rate of fire and ammo properties.
//Fire the plasma pistol several times and display the amount of ammo remaining.
//reload the plasma pistol
system("pause");
return 0;
}
plasmaPistolClass::plasmaPistolClass(){
//default constructor
//intialize the properties
}
plasmaPistolClass::plasmaPistolClass(int powerSettings, int allowedAmmo){
//constructor
//intialize the properties
//set values of properties
int ammo = 30;
int rateOfFire = 5;
int maxAmmo = 30;
int destructivePower = 5;
bool safetyOn = false;
}
void plasmaPistolClass::pressTrigger(void){
if (safetyOn != true && rateOfFire <= ammo) //simulates pistol being fired
{
cout << "Pistol Fired " << rateOfFire << " bolts" << endl;
ammo -= rateOfFire; //decreases the ammo by rate of fire
cout << "Pistol Fired " << rateOfFire << " bolts" << endl;
ammo -= rateOfFire;
}
else
{cout << "pistol couldn't fire";} //will not fire is safety is on
}
void plasmaPistolClass::load(int nmbrOfBolts){
//loads ammo into the plasma pistol
//will only load ammo to plasma pistol's max
}
void plasmaPistolClass::setDestructivePower(int powerSetting){
//set the destructive power to max 10 to min of 1
constint min = 1, max = 10;
if (powerSetting >= min && powerSetting <= max) {
destructivePower = powerSetting;
}
else {
cout << "Cannot set the plasma pistol to that power setting";
}
}
int plasmaPistolClass::showDestructivePower(void){
//returns the destructive power of plasma pistol
return destructivePower;
}
void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress){
//sets the number of bolts fired per trigger pull
constint min = 1, max = 10;
boltsPerTriggerPress = 5;
if (boltsPerTriggerPress >= min && boltsPerTriggerPress <= max)
{
rateOfFire = boltsPerTriggerPress;
}
else
{cout << "Cannot set rate of fire to that setting";}
}
int plasmaPistolClass::showRateOfFire(void){
//returns the value of the rateOfFire property
return rateOfFire;
}
int plasmaPistolClass::ammoRemaining(void){
//displays the amount of ammo remaining in the pistol
return ammo;
}
You can call them similarly to how you called pistolRight.pressTrigger() in main(). To get the output you are wanting, in main(), or wherever, put code something like:
One thing about your code I don't understand is why if the rate of fire is 5, your pistol uses that value to squeeze off 10 rounds. You could modify that in your plasmaPistolClass::pressTrigger() function if you want.
plasmaPistolClass::plasmaPistolClass(){
//default constructor
//intialize the properties
}
plasmaPistolClass::plasmaPistolClass(int powerSettings, int allowedAmmo){
//constructor
//intialize the properties
//set values of properties
int ammo = 30;
int rateOfFire = 5;
int maxAmmo = 30;
int destructivePower = 5;
bool safetyOn = false;
}
Firstly, your default constructor is not setting any of the values to things like ammo, rateOfFile etc.
Secondly, you are re-declaring the variables in your Nth constructor. int ammo = 30; should be ammo = 30; because the variable ammo already exists.