Question: Question 2: Download the file monsterType.cpp from Canvas. Use this file to create a project in Visual Studio. Read and understand the code. Imagine youre
Question 2:
Download the file monsterType.cpp from Canvas. Use this file to create a project in Visual Studio. Read and understand the code.
Imagine youre writing a game. The player encounters various monsters. There could be hundreds of monster types, but we only have two: Derived from the base class MonsterType is GhostType and ZombieType.
Notice that MonsterType is abstract. In our game, we dont expect to instantiate a monster that isnt a specific kind of monster. The MonsterType base class provides general attributes that are common among all monsters. In our program, all monsters have a name. The base class also provides instructions for things that must be implemented in derived classes. In our program, all monsters must support a fight() function. But every type of monster fights differently, so those operations are intended to be implemented in derived classes.
To make the program more spontaneous, the user encounters the different types of monsters in a randomized order. The order in which each monster is generated is determined at run-time. At compile-time, the order in which the monsters will be generated remains undetermined.
We can achieve this by implementing the following tasks in the main() function:
MonsterType.cpp:
#include
#include
#include
#include
using namespace std;
class MonsterType
{
public:
MonsterType(string n);
virtual void fight() = 0;
protected:
string name;
};
MonsterType::MonsterType(string name)
{
this->name = name;
}
class GhostType : public MonsterType
{
public:
GhostType(string name, string sound);
void fight();
private:
string sound;
};
GhostType::GhostType(string name, string sound)
: MonsterType(name)
{
this->sound = sound;
}
void GhostType::fight()
{
cout << "Enter " << name << ". " << sound << "! ";
}
class ZombieType : public MonsterType
{
public:
ZombieType(string name, string sound);
void fight();
private:
string sound;
};
ZombieType::ZombieType(string name, string sound)
: MonsterType(name)
{
this->sound = sound;
}
void ZombieType::fight()
{
cout << "Enter " << name << ". " << sound << "! ";
}
const int LENGTH = 20;
int main()
{
/// TODO: Create an array of 20 base class pointers because
/// there will be 20 monsters generated. Some will be
/// zombies. Some will be ghosts.
///
srand(static_cast
for (int i = 0; i < LENGTH; i++)
{
if (rand() % 2 == 0)
/// TODO: Use monsterPtr at the current index to point to a
/// new GhostType object. You can pass in the name Ghost
/// and the sound Boo using the parameterized constructor
///
else
/// TODO: Use monsterPtr at the current index to point to a
/// new ZombieType object. You can pass in the name Zombie
/// and the sound Chomp using the parameterized constructor
///
}
for (int i = 0; i < LENGTH; i++)
{
cout << i + 1 << " ";
/// TODO: Use base class monsterPtr to invoke the
/// polymorphic-determined monsters fight()
/// function here.
}
/// To avoid memory leaks, this loop releases the memory
/// for each monster that was allocated above.
for (int i = 0; i < LENGTH; i++)
delete monsterPtr[i];
return 0;
}
Tasks:
Create an array of 20 base class pointers because there will be 20 monsters generated. Some will be zombies. Some will be ghosts. Use this syntax: MonsterType *monsterPtr[20];
The following if-else structure is provided in main() to randomize whether a monster will be a zombie or ghost: if (rand() % 2 == 0)
If the if-block runs, use monsterPtr at the current index to point to a new GhostType object. You can pass in the name Ghost and the sound Boo using the parameterized constructor.
If the else-block runs, use monsterPtr at the current index to point to a new ZombieType object. You can pass in the name Zombie and the sound Chomp using the parameterized constructor.
for (int i = 0; i < LENGTH; i++)
{
if (rand() % 2 == 0)
// TODO: a. Create that new ghost object here.
else
// TODO: b. Create that new zombie object here.
}
Now we will loop through our array of monsters and call the fight() function. We dont know whether to call the fight() function on a ghost object or whether to call the fight() function on a zombie object. But we dont have to know. Because fight() is specified as virtual in the base class, our base class monsterPtr can be used to invoke those virtual functions of the derived classes.
for (int i = 0; i < LENGTH; i++)
{
cout << i + 1 << " ";
// TODO: Use base class monsterPtr to invoke the // polymorphic-determined monsters fight() // function here.
}
Upload your modified MonsterType.cpp file to Canvas. After submitting, please check your files (for question 1 and question 2) to ensure youve uploaded what you intended. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
