Question

In: Computer Science

C++ project should be completed with the following: Base called Character with common variables like m_health,...

C++ project should be completed with the following:

  1. Base called Character with common variables like m_health, m_score and m_damage and functions like Attack(), Heal(), Get() and Set() functions.
  2. Derive a Player class from Character class with a variable called m_name, derive an Enemy class from Character class with a variable called m_ID.
  3. Make proper use of public, private and protected.
  4. Use Constructors to set default values and Destructors to delete dynamically allocated variables.
  5. Make the Attack() and Heal() in the base class pure virtual functions, and override these functions in the derived class (Player and Enemy).
  6. Use new and delete when creating objects of the Player and Enemy class.

Use exception handling (try, catch and throw) when creating the objects of the player and enemy class.

ref:

#include <iostream>
#include "Player.h"
#include "Enemy.h"


int main() {

   Character* c[2];
   // Dynamically allocate the objects using new

   c[0]->Attack(*c[1]);
   c[1]->Attack(*c[0]);

   c[0]->Heal();
   c[1]->Heal();

   for (int i = 0; i < 2; ++i) {
       std::cout << c[i]->GetHealth() << std::endl;
   }

   // delete the character from the memory

   system("pause");
   return 0;
}

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Character.h

#ifndef CHARACTER_H
#define CHARACTER_H

#include <string>

using namespace std;
class Character
{
public:
Character(int m_health );

void setHealth( int health );
int getHealth();

   void setScore( int score );
int getScore();

void setDamage(int damage );
int getDamage();
   void Attack(Character& c);
   void Heal();
protected:
int m_health;
   int m_score;
   int m_damage;
};

#endif // CHARACTER_H

character.cpp

#include "Character.h"

Character::Character(int health ) :
   m_health( health )
{
}

void Character::setHealth( int health )
{
   m_health = health;
}

int Character::getHealth()
{
   return m_health;
}


void Character::setScore( int score )
{
   m_score = score;
}

int Character::getScore()
{
   return m_score;
}

void Character::setDamage( int damage )
{
   m_damage = damage;
}

int Character::getDamage()
{
   return m_damage;
}

void Character::Attack(Character& c){
   int temp;
   temp=c.getHealth()-m_damage;
   c.setHealth(temp);
}
void Character::Heal(){
   m_health+=10;
}

player.h

#ifndef PLAYER_H
#define PLAYER_H

#include <string>
#include "Character.h"
using namespace std;

class Player : public Character
{
private:
   string name;
public:
Player(string name, int health = 100 );
   void setName(string name );
string getName();
};

#endif // PLAYER_H

player.cpp

#include "Player.h"


Player::Player(string name , int health ) :
Character( health )
{
   setName(name);
   setDamage(20);
   setScore(0);
}

void Player::setName( string name )
{
   this->name = name;
}

string Player::getName()
{
   return name;
}

Enemy.h

#ifndef ENEMY_H
#define ENEMY_H

#include <string>
#include "Character.h"

class Enemy : public Character
{
private:
   string ID;
public:
Enemy(string ID, int health = 100 );
   void setID(string );
string getID();
};

#endif // ENEMY_H

Enemy.cpp

#include "Enemy.h"

Enemy::Enemy( string id , int health ) :
Character(health )
{
   setID(id);
   setDamage(10);
   setScore(0);
}

void Enemy::setID( string id )
{
   this->ID = id;
}

string Enemy::getID()
{
   return ID;
}

main.cpp

#include <iostream>
#include "Player.h"
#include "Enemy.h"

using namespace std;
int main() {
try{
Character* c[2];
// Dynamically allocate the objects using new
Player* p1=new Player("Player 1");
Enemy* e1=new Enemy("ID1");
c[0]=p1;
c[1]=e1;

c[0]->Attack(*c[1]);
c[1]->Attack(*c[0]);

c[0]->Heal();
c[1]->Heal();

for (int i = 0; i < 2; ++i) {
cout << c[i]->getHealth() << endl;
}

// delete the character from the memory
}catch(exception exp){
   cout<<"Something went wrong!!!\n";
}
system("pause");
return 0;
}

Output:


Related Solutions

In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
C++ Project Organization Create a project called Project1 and then name and organize the following programs...
C++ Project Organization Create a project called Project1 and then name and organize the following programs under it. Part 1 CircleArea You are going to write a program to compute and output the area of a circle with a radius of 2.5. Think through what you need to do: Create a variable for radius set it to 2.5 Create a variable for area set it to 3.14159 * radius * radius output the value of area Challenge (not required) Generalize...
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the...
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the index of wanted letter in string. For example, strfind("hello world", 'o') would return 4. If the character is not found, the funtion returns -1. Please code in c++
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT