Question
Character.cpp/hpp given: Character.cpp: #include Character.hpp /** Default constructor. Default-initializes all private members. Default character name: NAMELESS. Booleans are default-initialized to False. Default enum value: NONE.
Character.cpp/hpp given:
Character.cpp:
#include "Character.hpp"
/**
Default constructor.
Default-initializes all private members.
Default character name: "NAMELESS".
Booleans are default-initialized to False.
Default enum value: NONE.
Default Vitality, Max Armor, and Level: 0.
*/
Character::Character():name_{"NAMELESS"},race_{NONE},vitality_{0},armor_{0},level_{0},enemy_{false}
{
}
/**
Parameterized constructor.
@param : The name of the character (a string in UPPERCASE)
@param : The race of the character (a string)
@param : The character's vitality (a non-negative integer) , with default value 0
@param : The character's max armor level (a non-negative integer), with default value 0
@param : The character's level (a non-negative integer), with default value 0
@param : A flag indicating whether the character is an enemy, with default value false
@post : The private members are set to the values of the corresponding parameters.
Hint: Notice the default arguments in the parameterized constructor.
If the given parameters are invalid, the default values are used instead.
*/
Character::Character(const std::string& name, const std::string& race, int vitality, int armor, int level, bool enemy) : Character()
{
setName(name); // checks valid name
setRace(race); // checks valid race
(vitality >= 0) ? vitality_ = vitality: vitality_ = 0;
(armor >= 0) ? armor_ = armor: armor_ = 0;
(level >= 0) ? level_ = level: level_ = 0;
enemy_ = enemy;
}
/**
@param : the name of the Character
@post : sets the Character's title to the value of the parameter, in UPPERCASE. Only alphabetical characters are allowed. For example, attempting to create a character named "TW3EDLEDUM2" should create a character named "TWEDLEDUM".
: If the given parameter does not have any valid alphabetical characters, the character's name should be set to "NAMELESS".
*/
void Character::setName(const std::string& name)
{
std::string nameUpper = "";
for(int i = 0; i
if(std::isalpha(name[i]))
{
nameUpper+= toupper(name[i]);
}
}
if(nameUpper != "")
{
name_ = nameUpper;
}
else
{
name_ = "NAMELESS";
}
}
/**
@return : the name of the Character
*/
std::string Character::getName() const
{
return name_;
}
/**
@param : the race of the Character (a string)
@post : sets the Character's race to the value of the parameter. Valid races: [HUMAN, ELF, DWARF, LIZARD, UNDEAD]
: If the given race was invalid, set race_ to NONE.
*/
void Character::setRace(const std::string& race)
{
if(race == "HUMAN")
{
race_ = HUMAN;
}
else if(race == "ELF")
{
race_ = ELF;
}
else if(race == "DWARF")
{
race_ = DWARF;
}
else if(race == "LIZARD")
{
race_ = LIZARD;
}
else if(race == "UNDEAD")
{
race_ = UNDEAD;
}
else
{
race_ = NONE;
}
}
/**
@return : the race of the Character (a string)
*/
std::string Character::getRace() const
{
if (race_ == Race::HUMAN)
{
return "HUMAN";
}
else if (race_ == Race::ELF)
{
return "ELF";
}
else if (race_ == Race::DWARF)
{
return "DWARF";
}
else if (race_ == Race::LIZARD)
{
return "LIZARD";
}
else if (race_ == Race::UNDEAD)
{
return "UNDEAD";
}
else
{
return "NONE";
}
}
/**
@param : an integer vitality
@pre : vitality >= 0 : Characters cannot have negative health
@post : sets the vitality private member to the value of the parameter
*/
void Character::setVitality(const int& vitality)
{
if (vitality >= 0)
{
vitality_ = vitality;
}
}
/**
@return : the value stored in vitality_
*/
int Character::getVitality() const
{
return vitality_;
}
/**
@param : an integer armor level
@pre : armor >= 0 : Characters cannot have negative armor
@post : sets the armor private member to the value of the parameter
*/
void Character::setArmor(const int& armor)
{
if (armor >= 0)
{
armor_ = armor;
}
}
/**
@return : the value stored in armor_
*/
int Character::getArmor() const
{
return armor_;
}
/**
@param : an integer level
@pre : level >= 0 : Characters cannot have a negative
@post : sets the level private member to the value of the parameter
*/
void Character::setLevel(const int& level)
{
if (level >= 0)
{
level_ = level;
}
}
/**
@return : the value stored in level_
*/
int Character::getLevel() const
{
return level_;
}
/**
@post : sets the enemy flag to true
*/
void Character::setEnemy()
{
enemy_ = true;
}
/**
@return true if the character is an enemy, false otherwise
Note: this is an accessor function and must follow the same convention as all accessor functions even if it is not called getEnemy
*/
bool Character::isEnemy() const
{
return enemy_;
}
Character.hpp:
8 #ifndef CHARACTER_HPP_ 9 #define CHARACTER_HPP_ 10 #include 11 #include 12 #include 13 14 enum Race 15 { 16 NONE, HUMAN, ELF, DWARF, LIZARD, UNDEAD 17 }; 18 19 class Character 20 { 21 22 23 24 25 26 27 28 29 30 public: /** */ Default constructor. Default-initializes all private members. Default character name: "NAMELESS". Booleans are default-initialized to False. Default enum value: NONE. Default Vitality, Max Armor, and Level: 0. Character(); 31 32 33 /*** Parameterized constructor. 34 @param The name of the character (a string in UPPERCASE) 35 @param 36 @param 37 @param 38 @param : The character's level (a non-negative integer), with default value 0 39 40 @param @post 41 42 */ 43 The race of the character (a string) The character's vitality (a non-negative integer), with default value : The character's max armor level (a non-negative integer), with default value A flag indicating whether the character is an enemy, with default value false The private members are set to the values of the corresponding parameters. Hint: Notice the default arguments in the parameterized constructor. Character (const std::string& name, const std::string& race, int vitality = 0, int armor = 0, int level = 0, bool enemy = false); 44 45 /** 46 @param the name of the Character 47 48 */ 49 @post :sets the Character's title to the value of the parameter, in UPPERCASE. Only alphabetical characters are allowed. void setName(const std::string& name); 50 51 52 /** 53 54 */ 55 @return the name of the Character std::string getName() const; 56 57 58 /** 59 @param 60 @post 61 62 */ 63 the race of the Character (a string) sets the Character's race to the value of the parameter If the given race was invalid, set race to NONE. void setRace (const std::string & race);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started