Plasma Pistal using Class's

I am having a little problem with class's. I am tring to make a plasma pistol display something simular to this:

Destructive Power : 5
Number of bolts left : 30
Rate of fire : 5

Shooting 5 bolts
Shooting 5 bolts

Number of bolts left: 20

I am not really sure how to call these. I have some of the code done but I am not sure how to move forward. Any help would be great. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>

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 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
	const int 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
	const int 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;
}
Last edited on
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:

1
2
3
4
5
6
7
cout << "Destructive Power: " << pistolRight.showDestructivePower() << endl;
cout << "Number of bolts left: " << pistolRight.ammoRemaining() << endl;
cout << "Rate of fire: " << pistolRight.showRateOfFire() << endl;
cout << endl;
pistolRight.pressTrigger();
cout << endl;
cout << "Number of bolts left: " << pistolRight.ammoRemaining() << endl;


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.

~psault
Last edited on
I added the code you stated above and all the value show as

Destructive Power : -858993460
Number of bolts left : -858993460
Rate of fire : -858993460

Shooting -858993460 bolts
Shooting -858993460 bolts

Number of bolts left: 858993460

any idea how I would fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
ok I see now thanks for the tips
No worries :)
Topic archived. No new replies allowed.