In: Computer Science
c++ code please
show differint h files and cpp files
2.3 Task 1
You are working as a programmer designing a vehicular system for the newly forced Edison Arms Company. The weapon system is a generic weapon housing that can fit a number of different weapons that are all controlled by a tank in the same way. Most importantly, is the emphasis on safety. During combat, the weapon systems cannot become unreliable or fail lest the pilots be put in unnecessary danger. The weapons generate heat which will also be a factor. Therefore you will need to provide mechanisms to combat this.
2.3.1 fireControl
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 defined as follows:
fireControl -weapons:weapon ** -numWeapons: int --------------------------- +fireControl(numWeapons:int, weaponList: string *) +~fireControl()
+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 defined in the hierarchy.
numWeapons: The number of weapons that are mounted into the system. The class methods are as follows:
fireControl: This is the constructor. It will receive a list of weapon names 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. If the name of the weapon contains missile, then a missile type weapon must be created and similarly for laser. This should not be case sensitive. For example given the string [”Laser Beam”, ”laser rifle”,”missile pod”, ”missiles”], 4 weapons would be created, the first two being of the class laser, and the last two of the class missile. The default strength for a laser weapon should be set to 5 when creating laser weapons.
∼fireControl: 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.3.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 specifically 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 fireControl class as a struct with public access. You will need to research how to specify exceptions due to the potential for a ”loose throw specifier error” and what clashes this might have with a compiler. Remember that implementations of classes and structs must be done in .cpp files.
As a hint, all of the exception structs in this practical will have three functions:
• A constructor
• a virtual destructor with a throw() specifier
• a const what function which returns a const char* with a throw()
specifier.
2.3.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 specifically 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 specifier error” and what clashes this might have with a compiler. Remember that implementations of classes and structs must be done in .cpp files.
2.3.4 Weapon Parent Class
This is the parent class of laser and missile. Both of these
classes inherit publicly from
it. It is defined according to the following UML diagram:
weapon -ammo:int -type:string -name: string ------------------- +weapon() +weapon(a:int, t:string, n:string) +getAmmo():int +setAmmo(a:int):void +getType():string +setType(s:string):void +getName():string +setName(s:string):void +ventWeapon(heat:T):void
+∼weapon()
+fire()=0:string
The class variables are as follows:
• ammo: The amount of ammo stored in the weapon. As it is fired, this will deplete. • type: The type of the weapon as a string which relates to its class.
The class methods are as follows:
weapon: The default class constructor.
weapon(a:int, t:string, n:string): The constructor. It will take three arguments and instantiate the class variables accordingly with name being the last variable set.
getAmmo/setAmmo: The getter and setter for the ammo.
getType/setType: The getter and setter for the type.
getName/setName: The getter and setter for the name.
∼weapon: The destructor for the class. It is virtual.
fire: This is the method that will fire each of the weapons and produce a string of the outcome. It is virtual here.
ventWeapon: This is a template function. It will receive a generic parameter rep- resenting some amount of heat. When called the function should determine the number of cooling cycles needed for the weapon to cool based on the amount of heat that is passed in. For every 10 units of heat, 1 cycle will be needed. You need to display a number of lines of output (with new lines at the end) to represent this. For example, if there’s 50.83 heat passed or 50 heat passed in, the output should be:
Heat Cycle 1
Heat Cycle 2
Heat Cycle 3
Heat Cycle 4
Heat Cycle 5
Pay attention to the format of the output messages. If the heat is less than 10, then display with a newline at the end:
Insufficient heat to vent
2.3.5 laser
The ionCannon is defined as follows:
laser -strength:int ------------------------------ +laser(s:int) +~laser() +setStrength(s:int):void +getStrength():int +fire():string
The class variables are as follows:
• strength: The strength of the laser. Lasers get stronger the
longer they are fired.
The class methods are as follows:
laser: The class constructor. This receives an initial strength for the laser.
∼laser: This is the destructor for the laser. It prints out ”X Uninstalled!” without the quotation marks and a new line at the end when the class is deallocated. X refers to the name of the weapon. For example:
Tri Laser Cannon Uninstalled!
fire: If the laser still has ammo, it must decrease the ammo by 1. It will also increase the strength by 1. It will return the following string: ”X fired at strength: Y” where Y represents the strength before firing and X, the name of the weapon. 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. 2.3.6 missile
The missile is defined as follows:
missile ------------------------------ +missile() +~missile() +fire():string
The class methods are as follows:
missile: This is the constructor for the class.
∼missile: This is the destructor for the missile. It prints out ”X Uninstalled!” without the quotation marks and a new line at the end when the class is deallocated. X refers to the name of the weapon. For example:
Missile Rack Uninstalled!
fire: If the rifle still has ammo, it must decrease the ammo by 1. It will return the following string: ”X fired!”. X refers to the name of the weapon. Do not add quotation marks. If ammo is not available, instead throw the ammoOut exception.
You should use the following libraries for each of the
classes:
• fireControl: iostream, string, sstream, algorithm, cstring,
exception • weapon: string, iostream, exception, sstream
Your submission must contain fire- Control.h, fireControl.cpp, laser.h, laser.cpp, missile.h, missile.cpp, weapon.h, weapon.cpp,main.cpp.
Ans)
C++ Program
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;
}
If your satisfy above answer please give positive rating
or Like?
please don't dislike
Thank you!