I have two classes that intend to function the same but with different data members, as such:
class Character
{
protected:
string job;
int MAX_HP;
int HP;
int STR;
int DEX;
int INT;
public:
Character();
Character(string job,
int max_hp,
int str=0, int dex=0, int inte=0);
string get_name();
// get func and set func
int get_MAX_HP();
void add_MAX_HP(int MAX_HP);
int get_HP();
void add_HP(int HP);
int get_STR();
void add_STR(int STR);
int get_DEX();
void add_DEX(int DEX);
int get_INT();
void add_INT(int INT);
}
class Monster
{
protected:
string name;
int MAX_HP;
int HP;
int ATK;
int DEF;
public:
Monster(string name, int MAX_HP, int Shield, int Barrier, int ATK, int DEF);
string get_name();
int get_MAX_HP();
void add_MAX_HP(int MAX_HP);
int get_HP();
void add_HP(int HP);
int get_ATK();
void add_ATK(int ATK);
int get_DEF();
void add_DEF(int DEF);
}
These are all the difference between the two classes I have designed so far. As I am writing the other functionalities I start to realize that they work essentially the same despite the different data members, along with their getter and setter. I feel like making the two class declarations code into one. I think I can write a virtual base class for sure, that contains the union of all data members and member functions, and only initialize some members for Character
or Monster
. But can I achieve it with template? With template Character
and Monster
are instantiation of one template, but I am uncertain whether can template initialize different members in response to different typename
...
Edit:
Allow me to be more specific. The Character
and Monster
are designed to fight. It also allows the Character
and Character
, Monster
and Monster
to fight. With two classes, there will be four member function in total to design:
class Character{
void Fight(Character hero, Monster enemy);
void Fight(Character hero, Character enemy);
}
class Monster{
void Fight(Monster mon, Character enemy);
void Fight(Monster mon, Monster enemy);
}
I want to make the Fight()
function spread among two classes into one block to save code, as they all share using ATK/STR/DEX/INT
to deduct HP
of other object, DEF
deducted for Monster
. In finishing writing the function, I suspect a lot of copy & paste would be done, and code is already lengthy for getter and setter with similar functionalities, for each data member. I noticed a partial specialization for template. But it would be the same to write two separate code blocks for Character
and Monster
. Is it my class design that impedes merging homogeneous codes, or is it inevitable for C++, to write class with slight difference?