Classes private functions

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);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Hero.cpp
int Herolevel;
int HeroHp;
int HeroStrength;
int HeroAttack;
int HeroDefense;

Hero::Hero()
{
	cout << "HOLA! Hero Created using normal constructor\n";
}

Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
{
	cout << "Hero created using Overloaded function!\n";
	HeroHp = newHp;
	cout << "Hp is: "<< HeroHp << endl;
	Herolevel = newLevel;
	cout << "level is: " << Herolevel << endl;
	HeroAttack = newAttack;
	cout << "Attack is: " << HeroAttack << endl;
	HeroDefense = newDef;
	cout << "Defense is: " << HeroDefense << endl;
}


Hero::~Hero(void)
{
	cout << "Hero destroyed!\n";
}


1
2
3
4
5
6
7
8
9
10
11
//Monsters.h
class Monsters:
	public Characters //Hero
{

public:
	Monsters(void);
	Monsters(int, int, int, int);
	Monsters(int);
	~Monsters(void);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Monsters.cpp
int Monsterlevel;
int MonsterHp;
int MonsterStrength;
int MonsterAttack;
int MonsterDefense;

Monsters::Monsters(void)
{
	"Monster Created";
}

Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
{
	cout << "Monster created using Overloaded function!\n";
	MonsterHp = newHp;
	cout << "Hp is: "<< MonsterHp << endl;
	Monsterlevel = newLevel;
	cout << "level is: " << Monsterlevel << endl;
	MonsterAttack = newAttack;
	cout << "Attack is: " << MonsterAttack << endl;
	MonsterDefense = newDef;
	cout << "Defense is: " << MonsterDefense << endl;
}

Monsters::~Monsters(void)
{
	cout << "\nMonster Destroyed";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//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;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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.

1
2
3
4
class Hero: public Character
{
   // Stuff
};
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 
if you want to assign value use setAttack()
Oh, I see what you mean.

Is there any reason you can't just do it in their constructor?

Short example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

class Character
{
protected:
  int attack;
public:
  void PrintAttack()
  {
    std::cout << "Attack is " << attack << std::endl;
  }
};

class Hero: public Character
{
public:
  Hero()
  {
    attack = 5; 
  }
};

class Monster: public Character
{
public:
  Monster()
  { 
    attack = 3; 
  }
};

int main( int argc, char* argv[] )
{
  Hero h;
  Monster m;

  h.PrintAttack();
  m.PrintAttack();

  return 0;
}


Edit: This example could have been a bit nicer had I have used dynamic binding and memory but I left it this way for simplicity.
Last edited on
Ok, i tried using the setAttack() method but the get attack is still returning nothing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	Characters h;
	h.setAttack(5);
	h.getAttack();//Why isnt this working?

	Hero Me;
	Me.setAttack(10);
	Me.getAttack();//Why isnt this working?
	
	Monsters m;
	m.setAttack(20);
	m.getAttack();//Why isnt this working?

	cin.sync();
	cin.get();
	return 0;
}
It is returning something - you're just not doing anything with what it's returning.
Why isnt this working?
You need to do something with the returned value of getAttack().
What do you think should happen?
Ohhh! ok i got it now. Thanks guys! I thought that by returning a value it was automatically displayed.
Topic archived. No new replies allowed.