-
Just when I think I have it all figured out
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?
-
I think you have the answer.... just make the constructor:
plasmaPistolClass ()
{
ammo = 60;
rateOfFire = 3;
maxAmmo = 60;
destructivePower = 8;
safetyOn = false;
}
same as your constructor that has parameters (which, by the way, it does not seem to use???). IMO the default is this one, and you need to *use* your parameters in the other one to do something different (???).
-
As jonnin said, you must initialize your object's data members in the constructor. Preferably, it's done by using a mem-init list:
Code:
class plasmaPistolClass{
//...
public:
plasmaPistolClass():ammo (60),rateOfFire (3), maxAmmo(60),destructivePower (8), safetyOn (false) {}
};
Danny Kalev
-
Ya I got that finally. Thank you. So how are you guys at arrays? JK
-
 Originally Posted by LilProblems
Ya I got that finally. Thank you. So how are you guys at arrays? JK
Doing well, I reckon:)
Danny Kalev
Similar Threads
-
By oblivionph in forum Java
Replies: 2
Last Post: 01-03-2006, 10:30 AM
-
By Lithic in forum VB Classic
Replies: 2
Last Post: 02-26-2005, 08:14 PM
-
By Justin Law in forum .NET
Replies: 0
Last Post: 10-02-2001, 08:29 PM
-
By Brad Isaacs in forum Database
Replies: 0
Last Post: 05-28-2001, 06:24 PM
-
By Jason Kontkanen in forum VB Classic
Replies: 1
Last Post: 08-17-2000, 11:44 AM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks