28.9k views
4 votes
In this exercise, you will implement a basic RPG game with two character types and the possibility to play a game, that is, having two characters fight each other. Four classes are necessary in order to complete this assignment. These classes will be called Character, Barbarian, Mage, and Game.

Character Class

This class (for which the header file is provided) will have:

Variable called "name" of data type string (Private member)
Variable called "race" of data type string (Private member)
Variable called "level" of data type int (Private member)
Variable called "health" of data type int (Private member)
A parameterized constructor that takes name, race, level, and health
Public Accessor functions to access these variables
Public Mutator functions to change these variables
A public function called "Attack()" that takes a pointer to a character
A public function called "Print()" that prints out the variables in this format:
Character Status:
Name: Bob
Race: Human
Level: 10
Health: 100

1 Answer

4 votes

Answer:

// character.h

#ifndef CHARACTER_H

#define CHARACTER_H

#include <iostream>

#include <string>

using namespace std;

class Character{

private:

string name;

string race;

int level;

int health;

public:

// constructors

Character(string name_, string race_, int level, int health);

// getter functions

string getName() const;

string getRace() const;

int getLevel() const;

int getHealth() const;

// setter functions

void SetName(string name);

void SetRace(string name);

void SetLevel(int level);

void SetHealth(int health);

// other functions

virtual void Print();

virtual void Attack(Character *target) = 0;

};

#endif

//end of character.h

// character.cpp

#include <iostream>

#include <string>

#include "character.h"

using namespace std;

// constructor

Character::Character(string name_, string race_, int level, int health) : name(name_), race(race_), level(level), health(health)

{}

// getter functions

string Character::getName() const

{

return name;

}

string Character::getRace() const

{

return race;

}

int Character::getHealth() const

{

return health;

}

int Character::getLevel() const

{

return level;

}

// setter functions

void Character::SetHealth(int health_)

{

health = health_;

}

void Character::SetLevel(int level_)

{

level = level_;

}

void Character::SetName(string name_)

{

name = name_;

}

void Character::SetRace(string race_)

{

race = race_;

}

void Character::Print()

{

cout<<"Character Status: "<<endl;

cout<<"Name: "<<name<<endl;

cout<<"Race: "<<race<<endl;

cout<<"Level: "<<level<<endl;

cout<<"Health: "<<health<<endl;

}

//end of character.cpp

User Saurav
by
4.0k points