Now we\'ll write another class battleShip. If you\'re not familiar with the game
ID: 3731119 • Letter: N
Question
Now we'll write another class battleShip. If you're not familiar with the game, it might helpful to go read up on it before doing this question. https://en.wikipedia.ora/wiki/Battleship (aame) class battleshipt public: battleShip (string) battleShip) void setShipName (string): string getShipName () void setSize(int); int getsize); void recordHit(): bool isSunk(); Increments the hits data member /I returns true if hits is greater than equal to size private string name; int size; int hits; For this question, we've provided a complete class definition in the coderunner box Write the function definitions for all 8 member functions of the battleShip class.Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
class battleShip{
string name;
int size;
int hits;
public:
battleShip(string name)
{
this->name = name;
this->size = -1;
this->hits = 0;
}
void setShipName(string name)
{
this->name = name;
}
string getShipName()
{
return this->name;
}
void setSize(int size)
{
this->size = size;
}
int getSize()
{
return this->size;
}
void recordHit()
{
this->hit++;
cout<<this->name<<" has been hit "<<this->hits<<" times. ";
}
bool isSunk()
{
cout<<this->name<<" has been sunk.
return this->hit >= size;
}
};
int main()
{
battleShip ob("Destroyer");
cout<<"Name : "<<ob.getShipName()<<endl;
ob.recordHit();
ob.isSunk();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.