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: 3870587 • 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.

This is an assignment in c++ and I would like help with the bolded part only. I would like to know how to create an overload assignment operator for this and how to randomly generate 15 torpedo shots

Explanation / Answer

Solution==============================

Within your class SubMarine, use this method for overloading assinment operator

void operator = (SubMarine &sM ) {
depth = sM.getDepth();
surfaced = sM.getSurfaced();
setLength(sM.getLength());
setLocation(sM.getLocation());
setOrientation(sM.getOrientation());
sink(sM.isSunk());
}

Randomly generating 15 torpedo shots==========

int * fireTorpedos(){
   //Since a grid is 10X10, and each location on grid is represented by a number, 0-99
   //And since it's an adavanced submarine, its capaple of shooting at any angle
   //And can explode at any location(since its random)
   int shotLocations[15];
   int bodyStart=getLocation();
   int bodyEnd = bodyStart+ getLength();
  
   for(int i=0;i<15;i++){
       int shotAt=rand()%99; //Shot at any random location between 0-99
       //Keep trying another location until we get location outside our sub's body, which otherwise will be suicidal ;)
       while(shotAt >= bodyStart && shotAt <= bodyEnd){
           shotAt=rand()%99;
       }
      
       shotLocations[i]=shotAt;
   }
  
   //This array will contain all locations to which torpedos have been shot at..
   return shotLocations;
  
}

Feel free to change to adapt to your program's implementation.. Good Luck !