Question: C++ Exception handle add invalid argument exception overfill attempted --x darts added to nerfgun program below #include #include using namespace std; class nerfGun { private:

C++

Exception handle

add invalid argument exception "overfill attempted --x darts added" to nerfgun program below

#include

#include

using namespace std;

class nerfGun {

private:

string model;

int range;

int capacity;

int number_of_darts;

public:

string getModel() {

return model;

}

int getCapacity() {

return capacity;

}

int getDartCount() {

return number_of_darts;

}

void fire() {

number_of_darts--;

}

void reload(int quantity) {

if ((number_of_darts + quantity) <= 144) {

number_of_darts = number_of_darts + quantity;

}

}

nerfGun(string model, int range, int capacity, int number_of_darts) {

this->model = model;

this->range = range;

if (capacity <= 144) {

this->capacity = capacity;

}

this->number_of_darts = number_of_darts;

}

};

int main() {

nerfGun n("ZombieStrikeModel", 5, 10, 20);

cout << n.getModel() << endl;

cout << "Number of Darts: " << n.getDartCount() << endl;

n.fire();

cout << "Fire: " << n.getDartCount() << endl;

n.reload(7);

cout << "Reload by 7: " << n.getDartCount() << " " << endl;

nerfGun m("GladiatorBlasterModel2", 6, 12, 24);

cout << m.getModel() << endl;

cout << "Number of Darts: " << m.getDartCount() << endl;

m.fire();

cout << "Fire: " << m.getDartCount() << endl;

m.reload(7);

cout << "Reload by 7: " << m.getDartCount() << endl;

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!