In: Computer Science
C++ project should be completed with the following:
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;
}
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: