In: Computer Science
C++ Program : You are working as a programmer designing a space ship weapon system for the newly
forced Space Force. The weapon system is a generic weapon housing that can t a number
of dierent weapons that are all controlled by a ship pilot in the same way. Most im-
portantly, is the emphasis on safety. During combat, the weapon systems cannot become
unreliable or fail lest the pilots be put in unnecessary danger. Therefore you will need to
provide mechanisms to combat this.
2.2.1 weaponMount
This is the mounting system for the weapons. It can store a number of weapons and
control them as well as handle problems. It is dened as follows:
weaponMount
-weapons:weapon **
-numWeapons: int
---------------------------
+weaponMount(numWeapons:int, weaponList: string *)
+~weaponMount()
+accessWeapon(i:int):weapon *
The class variables are as follows:
weapons: A 1D array of weapon pointers. It must be able to accept any type of
weapon dened in the hierarchy.
numWeapons: The number of weapons that are mounted into the system.
The class methods are as follows:
weaponMount: This is the constructor. It will receive a list of weapons as a string
array plus the number of weapons. It must allocate memory for the weapons variable
and create a weapon based on the information provided by the string array. Create
one weapon of the type indicated at each index of the array. For example ["Doom
Cannon","Ion Cannon"] means create a doomCannon weapon at index 0 and so on.
The default strength for an ion cannon is 5.
weaponMount: The class destructor. It must deallocate all of the memory assigned
to the class.
accessWeapon: This receives an int which provides an index in the weapons list. It
will return the weapon that is stored at that position. If no such weapon is found
there, then throw a weaponFailure exception.
2.2.2 weaponFailure
This is a custom exception class used in the context of this system. It will inherit publicly
from the exception class. You will need to override specically the what method to return
the statement "Weapon System Failure!" without the quotation marks. The name of this
class is weaponFailure. This exception will be used to indicate a failure of the weapon
system. You will implement this exception in the weaponMount class as a struct with
public access. You will need to research how to specify exceptions due to the potential
for a "loose throw specier error" and what clashes this might have with a compiler.
Remember that implementations of classes and structs must be done in .cpp les.
2.2.3 ammoOut
This is a custom exception class used in the context of this system. It will inherit publicly
from the exception class. You will need to override specically the what method to
return the statement "Ammo Depleted!" without the quotation marks. The name of this
class is ammoOut. This exception will be used to indicate a depletion of ammunition
for a weapon. You will implement this exception in the weapon class as a struct with
public access. You will need to research how to specify exceptions due to the potential
for a "loose throw specier error" and what clashes this might have with a compiler.
Remember that implementations of classes and structs must be done in .cpp les.
2.2.4 Weapon Parent Class
This is the parent class of ionCannon and doomCannon. Both of these classes inherit
publicly from it. It is dened according to the following UML diagram:
weapon
-ammo:int
-type:string
-------------------
+weapon()
+weapon(a:int, t:string)
+getAmmo():int
+getType():string
+setAmmo(s:int):void
+setType(s:string):void
+weapon()
+fire()=0:string
The class variables are as follows:
ammo: The amount of ammo stored in the weapon. As it is red, this will deplete.
type: The type of the weapon as a string. Examples include: "Rail Ri
Cannon","Doom Cannon", "Missile Launcher" and "Ion Cannon".
The class methods are as follows:
weapon: The default class constructor.
weapon(a:int, t:string): The constructor. It will take two arguments and instantiate
the class variables accordingly.
getAmmo,setAmmo: The getter and setter for the ammo.
getType,setType: The getter and setter for the ammo.
weapon: The destructor for the class. It is virtual.
fire: This is the method that will re each of the weapons and produce a string of
the outcome. It is pure virtual here.
2.2.5 ionCannon
The ionCannon is dened as follows:
ionCannon
-strength:int
------------------------------
+ionCannon(s:int)
+~ionCannon()
+setStrength(s:int):void
+getStrength() const:int
+fire():string
The class variables are as follows:
strength: The strength of the ion cannon. Ion cannons get stronger the longer they
are red.
The class methods are as follows:
ionCannon: The class constructor. This receives an initial strength for the ion
cannon.
ionCannon: This is the destructor for the ion cannon. It prints out "Ion Cannon
Uninstalled!" without the quotation marks and a new line at the end when the class
is deallocated.
fire: If the cannon still has ammo, it must decrease the ammo by 1. It will also
increase the strength by 1. It will return the following string: "Ion Cannon red at
strength: X" where X represents the strength before ring. Do not add quotation
marks. If ammo is not available, instead throw the ammoOut exception. No new
lines should be added.
getStrength/setStrength: The getter and setter for the strength variable.
2.2.6 doomCannon
The doomCannon is dened as follows:
doomCannon
-charge:int
------------------------------
+doomCannon()
+~doomCannon()
+getCharge():int
+setCharge(s:int):void
+fire():string
The class methods are as follows:
doomCannon: This is the constructor for the class. It will set the charge to 0.
doomCannon: This is the destructor for the ion cannon. It prints out "Doom
Cannon Uninstalled!" without the quotation marks and a new line at the end when
the class is deallocated.
setCharge/getCharge: The getter and setter for the charge variable.
re: The cannon can only re if it has ammo and only charge if it has ammo as
well. The charging process does not require ammo. Firing it will reduce the ammo
by 1. However the doom cannon requires a charging up period. Since the doom
cannon starts with 0 charge, it will not re until it reaches 5 charge, inclusive of the
5. Therefore every time it is red, it will increase in charge by 1. If the charge is not
at 5, then return the string "DOOM CANNON CHARGING" without quotation
marks. If the cannon starts at 5 charge when red reset the charge back to 0 and
return the following string: "DOOM CANNON FIRED!". Do not add quotation
marks. If ammo is not available, instead throw the ammoOut exception. No new
lines should be added.
You will be allowed to use the following libraries: sstream,exception, cstring,string,
iostream. Your submission must contain weaponMount.h, weaponMount.cpp, ionCannon.h, ionCannon.cpp, doom-
Cannon.h, doomCannon.cpp,weapon.h, weapon.cpp, main.cpp
weapon.h file:
#pragma once
#include <iostream>
using namespace std;
class weapon {
private:
int ammo; // current amount of ammunition stored in
the weapon
string type; // type of weapon
public:
weapon(); // default constructor
weapon(int a, string t); // constructor
virtual ~weapon(); // virtual destructor
int getAmmo(); // returns amount of ammo stored in
weapon
string getType(); //returns type of weapon
void setAmmo(int a); // set new amount of ammo in
weapon
void setType(string t); //set new weapon type
virtual string fire() = 0; // pure virtual function
need to be implimented into sub classes
struct ammoOut : public exception {
virtual char const* what()
const;
};
};
weapon.cpp file:
#include "weapon.h"
weapon::weapon() {
ammo = 0;
type = "N/A";
}
weapon::weapon(int a, string t) {
ammo = a;
type = t;
}
weapon::~weapon() {
// uninstall weapon
// virtual destructor calls derived class's
destructor
}
int weapon::getAmmo() {
return ammo;
}
string weapon::getType() {
return type;
}
void weapon::setAmmo(int a) {
ammo = a;
}
void weapon::setType(string t) {
type = t;
}
char const* weapon::ammoOut::what() const {
return "Ammo Depleted!";
}
ionCannon.h file:
#pragma once
#include "weapon.h"
class ionCannon : public weapon{
private:
int strength; // strength of ion cannon
public:
ionCannon(int s); // constructor
~ionCannon(); // destructor
int getStrength(); // returns strength of ion
cannon
void setStrength(int s); // set new strength of ion
cannon
string fire(); // fires the weapon if it hase ammo
left
};
ionCannon.cpp file:
#include "ionCannon.h"
#include <string>
ionCannon::ionCannon(int s) {
// set initial strength of ion cannon
strength = s;
// set type of weapon
setType("Ion Cannon");
}
ionCannon::~ionCannon() {
// prints message
cout << "Ion Cannon Uninstalled!" <<
endl;
}
int ionCannon::getStrength() {
return strength;
}
void ionCannon::setStrength(int s) {
strength = s;
}
string ionCannon::fire() {
// check for ammo
if (getAmmo() > 0) {
// fire the weapon
setAmmo(getAmmo() - 1); // reduce
ammo by 1
//increase strength by 1
setStrength(getStrength() +
1);
// return message
string str = "Ion Cannon red at
strength: " + to_string(getStrength() - 1); // -1 as strength
represent before ring
return str;
}
else {
// throw ammoOut exception
throw ammoOut();
}
}
doomCannon.h file:
#pragma once
#include "weapon.h"
class doomCannon : public weapon {
private:
int charge; //
public:
doomCannon(); // constructor
~doomCannon(); // destructor
int getCharge(); // returns amount of charge
void setCharge(int s); // set new charge
string fire(); // fires doomCannon at charge 5 if it
hase ammo
};
doomCannon.cpp file:
#include "doomCannon.h"
doomCannon::doomCannon() {
//set initial charge to 0
charge = 0;
//set type of weapon
setType("Doom Cannon");
}
doomCannon::~doomCannon() {
// print message
cout << "Doom Cannon Uninstalled!" <<
endl;
}
int doomCannon::getCharge() {
return charge;
}
void doomCannon::setCharge(int s) {
charge = s;
}
string doomCannon::fire(){
// check for ammo
if (getAmmo() > 0) {
// red the doom cannon
//check for charge
if (charge == 5) {
// fire the
cannon
setCharge(0); //
set charge to 0 after cannon is fired
// reduce ammo
by 1
setAmmo(getAmmo() - 1);
return "DOOM
CANNON FIRED!";
}
else {
// charge the
cannon
// increase
charge by 1;
charge++;
return "DOOM
CANNON CHARGING";
}
}
else {
// throw ammoOut exception
throw ammoOut();
}
}
weaponMount.h file:
#pragma once
#include "ionCannon.h"
#include "doomCannon.h"
class weaponMount {
private:
weapon** weapons; // array of weapon pointers
int numWeapons; // number of weapon mountedd into the
system
public:
weaponMount(int _numWeapon, string* weaponList); //
constructor
~weaponMount(); // deconstructor
weapon* accessWeapon(int i); // returns weapon at ith
position
struct weaponFailure : public exception {
virtual char const* what()
const;
};
};
weaponMount.cpp file:
#include "weaponMount.h"
weaponMount::weaponMount(int _numWeapon, string* weaponList)
{
// takes weapon list string and creates numWeapon
weapon objects and stores in weapons array variable
numWeapons = _numWeapon; // initialize variables
// allocate memory for weapons
weapons = new weapon * [numWeapons];
// get individual weapon type from weaponList
for (int i = 0; i < numWeapons; i++) {
// get ith weapon from
weaponList
// create weapon according to type
and store it in weapons
if (weaponList[i].compare("Ion
Cannon") == 0) {
// create ion
cannon weapon
weapons[i] = new
ionCannon(5);
}
else if
(weaponList[i].compare("Doom Cannon") == 0) {
// create doom
cannon weapon
weapons[i] = new
doomCannon();
}
}
}
weaponMount::~weaponMount() {
//destroy all weapons and free the memomry
for (int i = 0; i < numWeapons; i++) {
delete weapons[i];
}
delete[] weapons;
}
weapon* weaponMount::accessWeapon(int i) {
// if weapon is not found throw weaponFailure
exception
if(i>=numWeapons){
throw weaponFailure();
}
else {
return weapons[i]; // return the
weapon
}
}
char const* weaponMount::weaponFailure::what() const {
return "Weapon System Failure!";
}
main.cpp file:
#include "weaponMount.h"
int main() {
cout << "create weaponMount system with 2
weapons" << endl;
string weaponlist[] = { "Doom Cannon","Ion Cannon"
};
weaponMount *wms = new weaponMount(2,
weaponlist);
cout << "Add 2 ammo to first weapon and 3 to
second weapon" << endl;
wms->accessWeapon(0)->setAmmo(2);
wms->accessWeapon(1)->setAmmo(3);
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire first weapon" << endl;
try {
cout <<
wms->accessWeapon(0)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "Get second weapon type: " <<
wms->accessWeapon(1)->getType() << endl;
cout << "fire second weapon" <<
endl;
try {
cout <<
wms->accessWeapon(1)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire second weapon" <<
endl;
try {
cout <<
wms->accessWeapon(1)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire second weapon" <<
endl;
try {
cout <<
wms->accessWeapon(1)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << endl;
cout << "fire second weapon" <<
endl;
try {
cout <<
wms->accessWeapon(1)->fire();
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << "fire third weapon" << endl;
try {
cout <<
wms->accessWeapon(2);
}
catch (const exception& e) {
cout << e.what() <<
endl;
}
cout << "Uninstall WeaponMount system" <<
endl;
delete wms;
return 0;
}