Question

In: Computer Science

Write the code of: – an abstract class called Creature – classes Player and Monster (derived...


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++

Solutions

Expert Solution

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;
}



Related Solutions

Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT