I want to be able to use my object of hero class to call the private function getAttack() from characters and generate a specific value just for that object. How can i do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//main
int main()
{
Characters h;//Created using normal constructor
h.getAttack();//i want this to lets say be 3
Hero Me;
Me.getAttack();//and this like 5 or something
Hero::Hero(1,2,3,4);//Created using overloaded constructor
Monsters m;
Monsters::Monsters(5,6,7,8);
cin.sync();
cin.get();
return 0;
}
1 2 3 4 5 6 7 8 9 10
//Hero.h
class Hero:
public Characters
{
public:
Hero();
Hero(int, int, int, int);
~Hero(void);
};
//Characters.h
class Characters
{
private:
int level;
int Hp;
int Strength;
int Attack;
int Defense;
public:
Characters(void);
Characters(int, int, int, int);
~Characters(void);
int getAttack();
int getDefense();
int getStrength();
int getHp();
int getLevel();
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setLevel(int);
friend Hero;
friend Monsters;
};
Characters::Characters(void)
{
cout << "\nCharacter has been created!\n";
}
Characters::~Characters(void)
{
cout << "Character has been destroyed!\n";
}
void Characters::setHp(int damage)//get Character left over hp
{
Hp -= damage;
}
int Characters::getAttack()
{
return Attack;
}
int Characters::getDefense()
{
return Defense;
}
int Characters::getStrength()
{
return Strength;
}
int Characters::getHp()
{
return Hp;
}
int Characters::getLevel()
{
return level;
}
The getAttack function of Characters isn't private; it's public.
I think the answer you're looking for is inheritance based. If you Hero inherits from the Character class, it will inherit any public or protected attributes and functions.
Oh sorry, i meant to say public. And what i mean is i made the hero and monsters class inherit the character class because they are both characters in the game im trying to make, but how can i assign values to the getAttack function of hero and monsters individually?
like for example:
1 2 3 4 5 6 7 8
Characters h;
h.getAttack();//i want this to lets say be 3
Hero Me;
Me.getAttack();//and this like 5 or something
Monsters m;
m.getAttack();// and this 10