Question

In: Computer Science

c++ code please show differint h files and cpp files 2.3 Task 1 You are working...

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.

Solutions

Expert Solution

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!


Related Solutions

Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes. Overview You will be writing the classes Grade and GradeCollection. You will be writing testing code for the Grade and GradeCollection classes. Part 1 – Create a Grade Class Write a class named Grade to store grade information. Grade Class Specifications Include member variables for name (string), score (double). Write...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT