Write a c++ program for the following UML introduction: You are working as a pro
ID: 3742623 • Letter: W
Question
Write a c++ program for the following UML
introduction:
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 ["Rail
Rie","Ion Cannon"] means create a railRie 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.
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 files.
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 files.
Weapon Parent Class
This is the parent class of ionCannon and railRifle. 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()
+re()=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 Rifle", "Laser
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.
re: This is the method that will re each of the weapons and produce a string of
the outcome. It is virtual here.
ionCannon
The ionCannon is dened as follows:
ionCannon
-strength:int
------------------------------
+ionCannon(s:int)
+~ionCannon()
+setStrength(s:int):void
+getStrength():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.
getStrength/setStrength: The getter and setter for the strength variable
railRifle
The railRie is dened as follows:
railRifle
------------------------------
+railRifle()
+~railRifle()
+fire():string
The class methods are as follows:
railRie: This is the constructor for the class.
railRie: This is the destructor for the ion cannon. It prints out "Rail Ri
Uninstalled!" without the quotation marks and a new line at the end when the class
is deallocated.
re: If the rie still has ammo, it must decrease the ammo by 1. It will return the
following string: "Rail Rie red!". Do not add quotation marks. If ammo is not
available, instead throw the ammoOut exception.
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, rail-
Rie.h, railRie.cpp, weapon.h, weapon.cpp,main.cpp
Explanation / Answer
ANSWER:
ionCannon.cpp
#include "ionCannon.h"
ionCannon :: ionCannon(int s){
strength = s;
}
ionCannon :: ~ionCannon(){
cout << "Ion Cannon Uninstalled!" << endl;
}
void ionCannon :: setStrength(int s){
strength = s;
}
int ionCannon :: getStrength() const{
return (strength);
}
string ionCannon :: fire(){
int strength_ = strength;
string myString;
stringstream s_s;
if(getAmmo() != 0){
setAmmo(getAmmo()-1);
strength++;
s_s << strength_;
myString = s_s.str();
return ("Ion Cannon fired at strength: " + myString);
}
else{
throw ammoOut();
}
}
ionCannon.h
#ifndef ionCannon_h
#define ionCannon_h
#include "weapon.h"
class ionCannon : public weapon{
private:
int strength;
public:
ionCannon(int s);
~ionCannon();
void setStrength(int s);
int getStrength() const;
string fire();
};
#endif
laserCannon.cpp
#include "laserCannon.h"
laserCannon :: laserCannon(char f){
firingMode = f;
}
laserCannon :: ~laserCannon(){
cout << "Laser Cannon Uninstalled!" << endl;
}
char laserCannon :: getMode(){
return (firingMode);
}
void laserCannon :: setMode(char s){
firingMode = s;
}
string laserCannon :: fire(){
if(getAmmo() != 0){
if(getAmmo() >= 1 && firingMode == 'S'){
setAmmo(getAmmo() -1);
return ("Laser Cannon fired!");
}
else
if(getAmmo() >= 4 && firingMode == 'Q'){
setAmmo(getAmmo()-4);
return ("Laser Cannon Quad Burst fired!");
}
else{
throw ammoOut();
}
}
else{
throw ammoOut();
}
}
laserCannon.h
#ifndef laserCannon_h
#define laserCannon_h
#include "weapon.h"
using namespace std;
class laserCannon : public weapon{
private:
char firingMode;
public:
laserCannon(char f);
~laserCannon();
char getMode();
void setMode(char s);
string fire();
};
#endif
weapon.cpp
#include "weapon.h"
weapon :: weapon(){}
weapon :: ~weapon(){}
weapon :: ammoOut :: ammoOut(){}
weapon :: ammoOut :: ~ammoOut() throw(){}
weapon :: weapon(int a, string t) :ammo(a), type(t){}
int weapon :: getAmmo(){
return (ammo);
}
string weapon :: getType(){
return (type);
}
void weapon :: setAmmo(int s){
ammo = s;
}
void weapon :: setType(string s){
type = s;
}
const char* weapon :: ammoOut :: what() const throw(){
return ("Ammo Depleted!");
}
weapon.h
#ifndef weapon_h
#define weapon_h
#include <sstream>
#include <exception>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
class weapon{
private:
int ammo;
string type;
public:
weapon();
weapon(int a, string t);
int getAmmo();
string getType();
void setAmmo(int s);
void setType(string s);
virtual ~weapon();
virtual string fire()=0;
struct ammoOut : public exception{
ammoOut();
virtual ~ammoOut() throw();
virtual const char* what() const throw();
};
};
#endif
weaponMount.cpp
#include "weaponMount.h"
weaponMount :: weaponFailure :: weaponFailure(){}
weaponMount :: weaponFailure :: ~weaponFailure() throw(){}
weaponMount :: ~weaponMount(){
for (int i = 0; i < numWeapons; i++){
delete weapons[i];
}
delete [] weapons;
weapons = 0;
}
weaponMount :: weaponMount(int numWeapons, string* weaponList){
this->numWeapons = numWeapons;
weapons = new weapon*[this->numWeapons];
for (int i = 0; i < (this->numWeapons); i++){
if(weaponList[i] == "Laser Cannon Q"){
weapons[i] = new laserCannon('Q');
}
else
if(weaponList[i] == "Laser Cannon S"){
weapons[i] = new laserCannon('S');
}
else
if(weaponList[i] == "Ion Cannon"){
weapons[i] = new ionCannon(5);
}
else{
weapons[i] = 0;
}
}
}
weapon* weaponMount :: accessWeapon(int i){
for (int i = 0; i < (this->numWeapons); i_++){
if(i_ == i){return (weapons[i_]);}
}
throw weaponFailure();
}
const char * weaponMount :: weaponFailure :: what() const throw(){
return ("Weapon System Failure!");
}
weaponMount.h
#ifndef weaponMount_h
#define weaponMount_h
#include "ionCannon.h"
#include "laserCannon.h"
class weaponMount{
private:
weapon** weapons;
int numWeapons;
public:
weaponMount(int numWeapons, string* weaponList);
~weaponMount();
weapon* accessWeapon(int i);
struct weaponFailure : public exception{
weaponFailure();
virtual ~weaponFailure() throw();
virtual const char* what() const throw();
};
};
#endif
#include "weapon.h"
#include "weaponMount.h"
int main(){
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.