Just when I think I have it all figured out


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Just when I think I have it all figured out

  1. #1
    Join Date
    Nov 2009
    Location
    Corona Ca
    Posts
    2

    Question 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?

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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 (???).

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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

  4. #4
    Join Date
    Nov 2009
    Location
    Corona Ca
    Posts
    2
    Ya I got that finally. Thank you. So how are you guys at arrays? JK

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by LilProblems View Post
    Ya I got that finally. Thank you. So how are you guys at arrays? JK
    Doing well, I reckon:)
    Danny Kalev

Similar Threads

  1. Replies: 2
    Last Post: 01-03-2006, 10:30 AM
  2. Dont worry...figured it out.
    By Lithic in forum VB Classic
    Replies: 2
    Last Post: 02-26-2005, 08:14 PM
  3. I figured out my problem...
    By Justin Law in forum .NET
    Replies: 0
    Last Post: 10-02-2001, 08:29 PM
  4. Thanks...figured it out!!!
    By Brad Isaacs in forum Database
    Replies: 0
    Last Post: 05-28-2001, 06:24 PM
  5. Re: Resizing windows (never mind, figured it out)
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center