First create the Codeblocks project Name your project Recita

First create the Codeblocks project. Name your project Recitation7.cpp. Now create the class file based on the instructions earlier in the recitation and name the class battleShip. When you create your class, you can set up your constructor and destructor at this time and it will populate these methods for you when it creates your class. The battleShip.h file has been made available on Moodle. Cut and paste the code from that file into the class you just created. You will then create your class file with a number of methods. (Remember: methods define what your object does). How many methods do you see in your header file? It will be your job to fill in these methods so that they work. In battleShip.cpp you will define the methods.The constructor always has the same name as your class file, and it never has a data type. Make sure to import the proper class file and libraries in the header of your battleShip.cpp file. You will also be given the main.cpp. Cut and paste the code from that file into the class you just created. Make sure to include the appropriate headers. See the main.cpp for further instructions. You will need to create 3 instances of your class, set up some of the methods, and expand on the logic of the while loop. battleShip.h will look like this: #ifndef BATTLESHIP_H #define BATTLESHIP_H #include using namespace std; class battleShip{ public: battleShip(string); ~battleShip(); void setShipName(string); string getShipName(); void setSize(int); int getSize(); void recordHit(); bool isSunk(); private: string name; int size; int hits; }; #endif // BATTLESHIP_H

//main.cpp

Solution

//battleShip.h
#ifndef BATTLESHIP_H
#define BATTLESHIP_H

#include <iostream>
using namespace std;

class battleShip{
public:
   battleShip(string);
   ~battleShip();

   void setShipName(string);
   string getShipName();

   void setSize(int);
   int getSize();

   void recordHit();
   bool isSunk();

private:
   string name;
   int size;
   int hits;
};
#endif // BATTLESHIP_H

--------------------------------------------------------------------------


//battleShip.cpp
#include<iostream>
#include<string>
#include \"battleShip.h\"
using namespace std;

battleShip::battleShip(string name)
{
   setShipName(name);
   hits=0;
   size=0;
}
battleShip::~battleShip()
{

}

void battleShip::setShipName(string)
{
   this->name=name;
}
string battleShip::getShipName()
{
   return name;
}

void battleShip::setSize(int size)
{
   this->size=size;
}
int battleShip::getSize()
{
   return size;
}

void battleShip::recordHit()
{
   hits=hits+1;
}
bool battleShip::isSunk()
{
   return size==hits;
}

--------------------------------------------------------------------------

//main.cpp
#include <iostream>
#include \"battleShip.h\"
using namespace std;
//main function
int main()
{
   //Declare 3 instances of the battleship class: Destroyer-0 Carrier-0 Cruiser-0

   battleShip ship_one(\"Destroyer\");
   battleShip ship_two(\"Carrier\");
   battleShip ship_three(\"Cruiser\");

   //Give the ships a size: Destroyer-3 Carrier-10 Cruiser-2
   ship_one.setSize(3);
   ship_two.setSize(10);
   ship_three.setSize(2);

   // Once you have this running for one, expand this while loop to include the other
   // two instances. Have the while loop end when all three ships have been sunk
   while(ship_one.isSunk() == false)
   {
       ship_one.recordHit();
   }
   cout<<\"Ship_one is sunk\"<<endl;

   while(ship_two.isSunk() == false)
   {
       ship_two.recordHit();
   }
   cout<<\"Ship_two is sunk\"<<endl;

   while(ship_three.isSunk() == false)
   {
       ship_three.recordHit();
   }
   cout<<\"Ship_three is sunk\"<<endl;

   system(\"pause\");
   return 0;
}

--------------------------------------------------------------------------

Sample output:

Ship_one is sunk
Ship_two is sunk
Ship_three is sunk

First create the Codeblocks project. Name your project Recitation7.cpp. Now create the class file based on the instructions earlier in the recitation and name t
First create the Codeblocks project. Name your project Recitation7.cpp. Now create the class file based on the instructions earlier in the recitation and name t
First create the Codeblocks project. Name your project Recitation7.cpp. Now create the class file based on the instructions earlier in the recitation and name t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site