I am just writing and playing around with some code and I wanted to have a word represent a number in the parameter list of my class object, my numbers are ints but if i wanted to use a float then I wouldn't be able to use an enum, so I created a struct and then initialized the variable in the parameter lists to keep things neater. Im wondering if this is ok to do. Everything works just fine but i've learned over the years that doesn't always mean it's correct practice.
Also I was wondering something about const. These variables in the parameter lists of these functions are declared as const:
Code:
void TakeDamage(const int& damageTaken); void RestoreHealth(const int& restoreAmount);
void UpgradeAttackPower(const int& upgradeAmount);
void DisplayCharacterInfo(Character& character);
I was told to make these const by someone else. Now i know that const makes it so the variable cannot be changed, but the damageTaken variable changes so why make them const? why does it still work even though they are declared as const?
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item
{
public:
Item();
string GetItemName() const { return itemName; }
private:
string itemName{};
int cost{};
int healthRestored{};
};
class Character
{
public:
Character(string charClass, int charHealth, int charMaxHealth, int charAttackPwr, int charMaxAttackPwr) :
charClass(charClass),
charHealth(charHealth),
charMaxHealth(charMaxHealth),
charAttackPwr(charAttackPwr),
charMaxAttackPwr(charMaxAttackPwr) {}
string GetCharClass() { return charClass; }
int GetCharHealth() const { return charHealth; }
int GetCharAttackPwr() const { return charAttackPwr; }
int GetCharMaxAttackPwr() const { return charMaxAttackPwr; }
int GetCharMaxHealth() const { return charMaxHealth; }
void TakeDamage(const int& damageTaken);
void RestoreHealth(const int& restoreAmount);
void UpgradeAttackPower(const int& upgradeAmount);
void DisplayCharacterInfo(Character& character);
private:
string charClass{};
int charHealth{};
int charMaxHealth{};
int charAttackPwr{};
int charMaxAttackPwr{};
};
ostream& operator<< (ostream& os, Character& character)
{
os << "Class Name: " << character.GetCharClass() << endl;
os << "Health: " << character.GetCharHealth() << endl;
os << "Max Health: " << character.GetCharMaxHealth() << endl;
os << "Attack Power: " << character.GetCharAttackPwr() << endl;
os << "Max Attack Power: " << character.GetCharMaxAttackPwr() << endl;
return os;
}
void Character::RestoreHealth(const int& restoreAmount)
{
if (charHealth + restoreAmount <= charMaxHealth)
{
charHealth += restoreAmount;
}
if (charHealth + restoreAmount >= charMaxHealth)
{
charHealth = charMaxHealth;
}
}
void Character::TakeDamage(const int& damageTaken)
{
if (charHealth > 0)
{
charHealth -= damageTaken;
if (charHealth <= 0)
{
charHealth = 0;
cout << "The " << GetCharClass() << ", was defeated." << endl;
}
}
}
void Character::DisplayCharacterInfo(Character& character)
{
cout << character << endl;
}
//Create a struct to give values a name instead of just a confusing number.
//Could have used an enum, but enums cant contain floats, so this fixes that potential issue.
struct Data
{
float health{};
int maxHealth{};
int attackPower{};
int maxAttackPower{};
};
int main()
{
Data data;
Character Knight("Knight", data.health = 100, data.maxHealth = 150, data.attackPower = 5, data.maxAttackPower = 5);
Character Samurai("Samurai", data.health = 50, data.maxHealth = 100, data.attackPower = 7, data.maxAttackPower = 7);
Character Marine("Marine", data.health = 200, data.maxHealth = 250, data.attackPower = 4, data.maxAttackPower = 4);
//Debug
cout << "--BEGIN DEBUG--\n" << endl;
Samurai.TakeDamage(Knight.GetCharAttackPwr());
Samurai.DisplayCharacterInfo(Samurai);
Knight.RestoreHealth(1);
Knight.DisplayCharacterInfo(Knight);
cout << "--END DEBUG--" << endl;
//End Debug
return 0;
}