Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; class MonsterType { public: MonsterType(string n); virtual void fight() = 0; protected: string name; }; MonsterType::MonsterType(string name) {

image text in transcribed
#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
}
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
}
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(time(NULL)));
for (int i = 0; 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
{
cout
/// 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
delete monsterPtr[i];
return 0;
}
Question 2: Download the file monsterType.cpp from Canvas. Studio Read and understand the code Use this file to create a project in Visual . Imagine you're writing a game The player encounters various monsters There could be hundreds of monster types, but we only have twe: Derived from the base class Monster Type is GhostType and Zombie Type Notice that MonsterType is abstract In our game, we don't expect to instantiate a monster thar isn't a specific kind of monster. The Monster Type base class provides general amnibutes that are common among all monsters. our progra all monsies have a name. The base class alse provides instructions for things that must be implementod in derivod classes. In our program, all moesicrs munt support a figh function. But every type of monster fights differety, so those operations are intended t be implemented in derived classes . To make the program more spontaneous, the user encoumers the different types od monsters in a randomizod order, The order in which cach monsder is gencraled is determined at run-time. At compile-time, the onder in which the moasters will be generated emains undetermined We can achieve this by implementing the following tasks in the main) function Tasks: I Crcale an array of 20 base class pointers because there will be 20 momlers generated Some will be zombies Some will be ghosts. Use this syntax 2 The followng ifel c structure ts provided in manto randomize wheth a monster will be anmbec or ghost: (rand() % 2 .. e) a. If the if-Hock rnns, use monsterPir at the current indes to point to a new GhosType ebject You ean pass in the name Ghes and the sound "Boo using the parameterized constructor lfthc clse-block nns, umnsterPtr at ZombieType object. You can pass in the name Zombie" and the sound "Chom asing the parameterized constructor b. current index to pot to a new for (let -LENGTH ) T00O-Create that new ghost object here T0OO b. Create that new zonbde object here Now we will loop through our array of monsters and call the fightD function We don't know whether to call the figh) function on a ghost objoct or whether to call the figha function on a zombie oject But we don't have to know Hecause fg) n specified an virtual in the base class, our hase class monsterPir can be used to invoke thse virtual functions of the derived clas polymorphic-deternined monsters fight) Upload your modified Monster Type.epp file lo Caax Affer sabmitting, please check you files(for question 1 and question 2) to ensure you've uploaded what you iniended Thank you

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions