I suggest you consider looking into the
this-> pointer. It basically points to the object itself, and I've changed your constructor to use it:
cpp
plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
this->ammo;
this->rateOfFire;
this->maxAmmo = maxBolts;
this->destructivePower = destructPower;
this->safetyOn = false;
}
Here is the complete code:
cpp
#include <iostream>
using namespace 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 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);
//The button class should 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
};
int main()
{
plasmaPistolClass pistol1();
plasmaPistolClass pistol2(5,20);
cout << "Destructive Power : " << pistol2.showDestructivePower() << endl;
cout << endl;
system("pause");
return 0;
}
plasmaPistolClass::plasmaPistolClass()
{
int maxAmmo = 50;
}
plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
this->ammo;
this->rateOfFire;
this->maxAmmo = maxBolts;
this->destructivePower = destructPower;
this->safetyOn = false;
}
void plasmaPistolClass::pressTrigger(void)
{
if (safetyOn != true && rateOfFire <= ammo)
{
cout << "Pistol Fired";
ammo -= rateOfFire;
}
else
{cout << "pistol couldn't fire";}
}
void plasmaPistolClass::setDestructivePower(int powerSetting)
{
if (powerSetting >= 1 && powerSetting <= 10)
{destructivePower = powerSetting;}
else
{cout << "Cannot set the plasma pistol to that power setting";}
}
int plasmaPistolClass::showDestructivePower(void)
{
if (destructivePower >= 1 && destructivePower <= 10)
{return destructivePower;}
}
void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress)
{
if (boltsPerTriggerPress >= 1 && boltsPerTriggerPress <= 10)
{rateOfFire = boltsPerTriggerPress;}
else
{cout << "Cannot set rate of fire to that setting";}
}
int plasmaPistolClass::showRateOfFire(void)
{
return rateOfFire;
}
int plasmaPistolClass::ammoRemaining(void)
{
return ammo;
}
Hope this helps
This post has been edited by gabehabe: 1 Jun, 2008 - 09:03 AM