HTML Code:#include <iostream> #include <string> using namespace std; class plasmaPistolClass{ private: int ammo; int rateOfFire; int destructivePower; public: bool safetyOn; int maxAmmo; void pressTrigger(void); void load(int nmbrOfBolts); void setDestructivePower(int powerSetting); int showDestructivePower(void); void setRateOfFire(int boltsPerTriggerPress); int showRateOfFire(void); int ammoRemaining(void); plasmaPistolClass(); plasmaPistolClass(int, int); }; int main(void) { plasmaPistolClass pistol1; plasmaPistolClass pistol2(8,60); cout << "Plasma Pistol 1 Destructive Power: " << pistol1.showDestructivePower() << endl; cout << "Plasma Pistol 1 Ammo Remaining: " << pistol1.ammoRemaining() << endl; cout << "Plasma Pistol 1 Rate of Fire: " << pistol1.showRateOfFire() << endl; cout << "Firing Pistol 1!" << endl; pistol1.pressTrigger(); cout << pistol1.showRateOfFire(); cout << endl; cout << "Firing Pistol 1!" << endl; pistol1.pressTrigger(); cout << pistol1.showRateOfFire(); cout << endl; cout << "Plasma Pistol 1 Ammo Remaining: " << pistol1.ammoRemaining() << endl; cout << endl; cout << "Plasma Pistol 2 Destructive Power: " << pistol2.showDestructivePower() << endl; cout << "Plasma Pistol 2 Ammo Remaining: " << pistol2.ammoRemaining() << endl; cout << "Plasma Pistol 2 Rate of Fire: " << pistol2.showRateOfFire() << endl; cout << "Firing Pistol 2!" << endl; pistol2.pressTrigger(); cout << pistol2.showRateOfFire(); cout << endl; cout << "Firing Pistol 2!" << endl; pistol2.pressTrigger(); cout << pistol2.showRateOfFire(); cout << endl; cout << "Plasma Pistol 2 Ammo Remaining: " << pistol2.ammoRemaining() << endl; cout << "Reloading Plasma Pistol 2"; pistol2.load(10); cout << endl; cout << "Plasma Pistol 2 Ammo Remaining: " << pistol2.ammoRemaining() << endl; cout << "Plasma Pistol 2 Destructive Power: " << pistol2.showDestructivePower() << endl; pistol2.setDestructivePower(8); cout << endl; cout << "Plasma Pistol 2 Destructive Power: " << pistol2.showDestructivePower() << endl; cout << "Setting Pistol 2 Rate Of Fire: "; pistol2.setRateOfFire(3); system("pause"); return 0; } plasmaPistolClass::plasmaPistolClass(void) { } plasmaPistolClass::plasmaPistolClass(int powerSettings, int allowedAmmo) { ammo = 60; rateOfFire = 3; maxAmmo = 60; destructivePower = 8; safetyOn = false; } void plasmaPistolClass::pressTrigger(void) { if (! safetyOn && rateOfFire < ammo) ammo -= rateOfFire; } void plasmaPistolClass::load(int nmbrOfBolts) { ammo += nmbrOfBolts; if (ammo > maxAmmo) ammo = maxAmmo; } void plasmaPistolClass::setDestructivePower(int powerSetting) { destructivePower = powerSetting; if (powerSetting > 8) destructivePower = 8; else if (powerSetting < 1); destructivePower = 1; } int plasmaPistolClass::showDestructivePower(void) { return destructivePower; } void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress) { rateOfFire = boltsPerTriggerPress; if (boltsPerTriggerPress > 9) boltsPerTriggerPress = 3; else if (boltsPerTriggerPress < 3); boltsPerTriggerPress = 3; } int plasmaPistolClass::showRateOfFire(void) { return rateOfFire; } int plasmaPistolClass::ammoRemaining(void) { return ammo; }
Here is my code. Everything compiles right and runs but the output for my first gun is coming out -858993460. I am guessing because I left my default constructor empty. But what can I do to fix this without having to rewrite my whole code?


Reply With Quote



Bookmarks