In: Computer Science
Write the code of:
– an abstract class called Creature
– classes Player and Monster (derived from Creature)
– classes WildPig and Dragon (derived from Monster)
In the Creature class
– Define a string member, Creature Name, to store the class
Creature’s name.(name should be dynamically allocated)
– Define Two virtual pure functions
• void DoAction() : Print the action of the object, and the actions
have to be different from different classes.
• void DrawOnScreen() : Print the object’s name and call DoAction()
belonging to the same class.
The class definition of Creature is:
Question: Implement the class Player, Monster, Dragon and Wildpig
so that when execution the following code, the counsel shows the
execution result as the following:
Counsel output:
Player <Kick_Ass> is attacking!!
Monster<UFO> is doing monster stuff!!
WildPig <I'm_Hungry> is Running!!
Dragon<I'm_the_Boss> is breathing Fire!!
Main function:
//==================================================
int main(){
Player hero("Kick_Ass");
Monster mon("UFO");
WildPig pig("I'm_Hungry");
Dragon drag("I'm_the_Boss");
Creature* object[4];
object[0]=&hero;
object[1]=&mon;
object[2]=&pig;
object[3]=&drag;
object[0]->DrawOnScreen();
object[1]->DrawOnScreen();
object[2]->DrawOnScreen();
object[3]->DrawOnScreen();
return0;
}
c++
main.cpp
//
// main.cpp
#include <iostream>
#include "Header.h"
#include <string>
using namespace std;
int main(){
Player hero("Kick_Ass");
Monster mon("UFO");
WildPig pig("Herman");
Dragon drag("Anton");
Creature* object[4];
object[0]=&hero;
object[1]=&mon;
object[2]=&pig;
object[3]=&drag;
object[0]->DrawOnScreen();
object[1]->DrawOnScreen();
object[2]->DrawOnScreen();
object[3]->DrawOnScreen();
return 0;
}
Header.h
#ifndef Header_h
#define Header_h
#include <string>
using namespace std;
class Creature{
public:
Creature() {}
Creature(string name);
virtual void DoAction()=0;
virtual void DrawOnScreen()=0;
protected:
string CreatureName;
};
class Player: public Creature {
public:
Player(string name);
void DoAction() override;
void DrawOnScreen() override;
};
class Monster: public Creature {
public:
Monster() {}
Monster(string name);
void DoAction();
void DrawOnScreen();
};
class WildPig: public Monster {
public:
WildPig(string name);
void DoAction();
void DrawOnScreen();
};
class Dragon: public Monster {
public:
Dragon(string name);
void DoAction();
void DrawOnScreen();
};
#endif /* Header_h */
Creature.cpp
#include "Header.h"
#include <iostream>
#include <string>
using namespace std;
Creature::Creature(string name) {
CreatureName = name;
}
Player::Player(string name){
CreatureName = name;
}
Monster::Monster(string name) {
CreatureName = name;
}
WildPig::WildPig(string name) {
CreatureName = name;
}
Dragon::Dragon(string name) {
CreatureName = name;
}
void Player::DoAction(){
cout << "is attacking!!";
}
void Monster::DoAction(){
cout << "is doing monster stuff!!";
}
void WildPig::DoAction(){
cout << "is running!!!";
}
void Dragon::DoAction(){
cout << "is breathing fire!!";
}
void Player::DrawOnScreen(){
cout << "Player <" <<
CreatureName << "> ";
DoAction();
cout << endl;
}
void Monster::DrawOnScreen(){
cout << "Monster <" <<
CreatureName << "> ";
DoAction();
cout << endl;
}
void WildPig::DrawOnScreen(){
cout << "WildPig <" <<
CreatureName << "> ";
DoAction();
cout << endl;
}
void Dragon::DrawOnScreen(){
cout << "Dragon <" <<
CreatureName << "> ";
DoAction();
cout << endl;
}
