Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a base class called WaterVehicle that has: length of ship (in number of g

ID: 3887875 • Letter: C

Question

Create a base class called WaterVehicle that has:

length of ship (in number of grid spaces)

starting grid location

horizontal or vertical orientation on grid

sunk (boolean)

Then create a class called Submarine that is derived from WaterVehicle and has the following additional properties:

dive depth

surfaced (Boolean)

Be sure your classes have a reasonable complement of constructors, accessor, and mutator methods including a public function to determine if the Submarine was hit by a torpedo and whether a hit sunk the ship. Additionally, create an overloaded assignment operator.

Write a program which will allow input of the ship length, grid location, orientation, and dive depth. Assume the grid where the submarine is to be placed is a 10 by 10 grid. Randomly generate 15 torpedo shots, output the shot grid locations to the screen (you could use the grid class from the last assignment to hold the shot locations), and then output whether the submarine was hit or missed or sunk. The submarine should be the object to understand whether it was hit or sunk. Then create a second submarine and use your overloaded assignment operator to assign the values from the first submarine to the second submarine. Print out the information about the second submarine.

Here is an Grid class from last assignment

#include<iostream>

#include <cstdlib> //Standard library

#include <ctime>

using namespace std;

//Class Grid

class Grid

{

//Declare private members function

private:

int x,y;

int **data;

  

//Declare public members function

public:

Grid(int m, int n)

{

x = m;

y = n;

data = new int*[m];

for (int i = 0; i<m; i++){

data[i] = new int[n];

}

}

  

void Fill()

{

  

int num = (x*y)/3;

for (int i = 0; i<=num; i++){

data[rand()%x][rand()%y] = 1;

}

}

  

int **getData()

{

return data;

}

void display()

{

for (int i=0; i<x; i++){

for(int j = 0; j<y; j++){

cout << data[i][j] << " ";

}

cout << endl;

}

}

};

int main()

{

int a,b;

cout << "Enter dimensions for grid (X and Y): " << endl;

cin >> a >> b;

srand (time(NULL));

Grid grid1(a,b);

Grid grid2(a,b);

Grid grid3(a,b);

grid1.Fill();

grid2.Fill();

for (int i = 0; i<a; i++){

for (int j = 0; j<b; j++){

if (grid1.getData()[i][j] == 1 && grid2.getData()[i][j] == 1)

grid3.getData()[i][j] = 1;

else

grid3.getData()[i][j] = 0;

}

}

  

//Display the grid as per the user input

cout << "The first grid is:" << endl;

grid1.display();

cout << "The second grid is:" << endl;

grid2.display();

cout << "The third grid is:" << endl;

grid3.display();

  

return 0;

}

Is there someone to help me on this problem to write code on C++ language?

Thanks in advance

Explanation / Answer

#include <iostream>

using namespace std;

class WaterVehicle {
private:
int length; //length of ship (in number of grid spaces)
int location; // starting grid location
string orientation; //horizontal or vertical orientation on grid
bool sunk; //
  
public:
// Constructor
WaterVehicle(int l, int loc, string o, bool s){
length = l;
location = loc;
orientation = o;
sunk = s;
}
// Accessors
int getLength(){
return length;
}
int getLocation(){
return location;
}
string getOrientation(){
return orientation;
}
bool isSunk(){
return sunk;
}
  
// Mutators
void setLength(int l){
length = l;
}
void setLocation(int loc){
location = loc;
}
void setOrientation(string o){
orientation=o;
}
void sink(bool s){
sunk = s;
}
};

class SubMarine:WaterVehicle{
private:
int depth;
bool surfaced;
public:
// Constructor
SubMarine(int l, int loc, string o, bool s,int d, bool surf):WaterVehicle(l,loc,o,s) {
depth = d;
surfaced = surf;
}
// Accessors
int getDepth(){
return depth;
}
bool getSurfaced(){
return surfaced;
}
// Mutators
void setDepth(int d){
depth = d;
}
void setSurfaced(bool s){
surfaced = s;
}
  
// Method to check if submarine is hit by torpedo
// (Location of torpedo < location of submarine + length of submarine and Location of torpedo > location of submarine - length of submarine)
bool isHitByTorpedo(int torpedoLocation){
int location = getLocation();
int length = getLength();
if ((torpedoLocation<(location+length))&& (torpedoLocation>(location-length))) {
return true;
}
}
  
void operator = (SubMarine &sM ) {
depth = sM.getDepth();
surfaced = sM.getSurfaced();
setLength(sM.getLength());
setLocation(sM.getLocation());
setOrientation(sM.getOrientation());
sink(sM.isSunk());
}
};

int main()
{
int depth, length, location;
string orientation;
cout<<"Enter depth of submarine: ";
cin>>depth;
cout<<"Enter length of submarine: ";
cin>>length;
cout<<"Enter location of submarine: ";
cin>>location;
cout<<"Enter orientation of submarine: ";
cin>>orientation;
  
SubMarine sM(length,location,orientation,false,depth,false);
return 0;
}