Classes Object's methods-inheritance!

I want to be able to make a battle scene and put it into a function, but every time i try i have to create objects in a different header file, which i then cant call in main. To try to fix this i made the loop in the main function but it says the object calling the method must be modifiable.

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
42
43
44
45
46
47
48
49
50
51
  //main.cpp
int main()
{
	Characters h;//Created using normal constructor
	cout << "Character: \n";
	h.setAttack(5);
	h.getAttack();
	h.setDefense(15);
	h.getDefense();
	Hero Me;
	cout << "Hero: \n";
	Me.setAttack(10);
	Me.getAttack();
	Me.setHp(5);
	Me.getHp();
	Hero::Hero(1,2,3,4);//Created using overloaded constructor
	Monsters m;
	cout << "Monster: \n";
	m.setAttack(20);
	m.getAttack();
	Monsters::Monsters(5,6,7,8);
	
//Problem is this this loop! i cant access the member functions for my objects.
//And i want to be able to put this in a funtion and call it from another file!
	do
	{
		cout << "Attacking!\n";
		cout << "Your hp is: " << Me.getHp() << endl;
		cout << "The enemy's hp is: "<< m.getHp << endl;
		cout << "\nThe monster has attacked you!\n";
		cout << "You received " << m.getStrength() << " damage;" << endl;
		Me.setHp() -= m.getStrength() ;
		cout << "\nYour hp is now: " << Me.getHp() << endl;
		cout << "Enemy hp is: "<< m.getHp << endl;
		cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
		m.setHp() -= Me.getAttack();
		cout << "Enemy hp is now: " << m.getHp() - Me.getAttack() << endl;
	} while ((Me.getHp() >= 0) && (m.getHp() >= 0));

	if ((Me.getHp > 0) && (m.getHp < 0))
		cout <<"Congratulations! You killed the enemy!" << endl;
	else if ((Me.getHp < 0) && (m.getHp > 0))
		cout << "You have died!" << endl;



	cin.sync();
	cin.get();
	return 0;
}

Here's the rest of my code.
1
2
3
4
5
6
7
8
9
10
11
12
//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
12
13
14
//Monsters.h
#pragma once
#include "Hero.h"
#include "Characters.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
//Characters.h
#pragma once
class Characters
{
private:
	int level;
	int Hp;
	int Strength;
	int Attack;
	int Defense;
public:
	Characters(void);
	Characters(int);
	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);
};


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Characters.cpp
Characters::Characters(void)
{
	cout << "\nCharacter has been created!\n";

}

Characters::Characters(int random)//How can i make this work?
{
	cout << "Level " << level << " character created with: \n";
	/*srand ((unsigned)time(0));
	random = rand() % 10 + 1;
	setlevel(int random);*/
	level = random;

}

Characters::~Characters(void)
{
	cout << "Character has been destroyed!\n";
}

void Characters::setAttack(int att)//get Character left over hp
	{
		Attack = att;
	}

void Characters::setDefense(int def)//get Character left over hp
	{
		Defense = def;
	}

void Characters::setStrength(int str)//get Character left over hp
	{
		Strength = str;
	}

void Characters::setHp(int damage)//get Character left over hp
	{
		Hp -= damage;
	}

void Characters::setlevel(int lvl)//get Character left over hp
	{
		level = lvl;
	}


int Characters::getAttack()
{
	cout << "Your attack is: " << Attack << endl;
	return Attack;
}

int Characters::getDefense()
{
	cout << "Your defense is: " << Defense << endl;
	return Defense;
}

int Characters::getStrength()
{
	cout << "Your strength is: " << Strength << endl;
	return Strength;
}

int Characters::getHp()
{
	cout << "Your hp is: " << Hp << endl;
	return Hp;
}

int Characters::getLevel()
{
	cout << "Your level is: " << level << endl;
	return level;
}
For example, this compiles an error because i cant change it. Me.setHp() -= m.getStrength() ;
You need to go back over how functions work... Methods are just functions with access to this. If you understand functions, you will understand methods much better.

What do you expect to happen when you code Me.serHp() -= m.getStreangth();, and why would it?

At first glance this seems like a syntax problem, so I think you should try writing a very simple program with just two functions (or methods) and try to get that to work first.
1
2
3
4
void Characters::setHp(int damage)//get Character left over hp
	{
		Hp -= damage;
	}


This function isn't returning anything so
 
Me.setHp() -= m.getStreangth()

doesn't make sense.

Instead, you just need to pass in the damage value into your setHp function.
 
Me.setHp(m.getStrength());


Also, I'd highly suggest renaming setHp to something different like takeDamage, to avoid confusion on what setHp does.

Good idea, i will change it! and i tried your suggestion, but when i compile it I get "Your hp is: -858993560" repeated four times!

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
Me.setHp(100);
	Me.setAttack(50);
	m.setHp(100);
	m.setStrength(30);
	cout << "boo";
	while ((Me.getHp() >= 0) && (m.getHp() >= 0))
	{	
		cout << "Attacking!\n";
		cout << "Your hp is: " << Me.getHp() << endl;
		cout << "The enemy's hp is: "<< m.getHp() << endl;
		cout << "\nThe monster has attacked you!\n";
		cout << "You received " << m.getStrength() << " damage;" << endl;
		Me.setHp(m.getStrength());
		cout << "\nYour hp is now: " << Me.getHp() << endl;
		cout << "Enemy hp is: "<< m.getHp() << endl;
		cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
		m.setHp(Me.getAttack());
		cout << "Enemy hp is now: " << m.getHp() - Me.getAttack() << endl;
	} 

	if ((Me.getHp() > 0) && (m.getHp() < 0))
		cout <<"Congratulations! You killed the enemy!" << endl;

	else if ((Me.getHp() < 0) && (m.getHp() > 0))
		cout << "You have died!" << endl;
Okay i finally fixed it, but now i have another problem! When i created my object it took those parameters and initialised them to it right? So why when i called their methods it returned a different value?
For example, I initialized them here while created my object at the same time

1
2
Hero Me(100,20,30,40);
Monsters m(100,16,18,20);


But when i called their get methods, it returned a different value. why?
For example,
1
2
Me.getHp();expected 100
m.getHp();expected 100


Instead of 100, it returned a super long negative number.

I even tried putting it in my battle loop, but as you guys can see, it came to no prevail. I had to re-initialize them again using the setHp() method. Can anyone tell me why it didn't initialize the first time?

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
42
43
44
45
46
47
48
49
50
51
int main()
{
	Hero Me(100,20,30,40);
	Monsters m(100,16,18,20);
//I had to initialize it here again, because the previous one wasn't working. why?
	Me.setAttack(89);
	Me.setHp(100);

	m.setAttack(40);
	m.setHp(100);


	cout << "\nAttacking!\n";

	while ((Me.getHp() > 0) && (m.getHp() > 0))
	{	
		cout << "\nYour hp is: " << Me.getHp() << endl;
		cout << "The enemy's hp is: "<< m.getHp() << endl;
		cout << "\nThe monster has attacked you!\n";
		cout << "You received " << m.getAttack() << " damage;" << endl;
		Me.damageTaken(m.getAttack());
		if(Me.getHp() > 0)//Check if still alive
		{
			cout << "\nYour hp is now: " << Me.getHp() << endl;
			//cout << "Enemy hp is: "<< m.getHp() << endl;
			cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() 
<< " Damage" << endl;
			m.damageTaken(Me.getAttack());

			if(m.getHp() > 0)//Check if still alive
			{
				cout << "Enemy hp is now: " << m.getHp() << endl;
				cout << "\nAttacking again!\n";
			}
		}

	} 
		if ((Me.getHp() > 0) && (m.getHp() <= 0))
			cout <<"\nCongratulations! You killed the enemy!" << endl;

		else if ((Me.getHp() <= 0) && (m.getHp() > 0))
				cout << "You have died!" << endl;
	



	cin.sync();
	cin.get();
	return 0;
}
Okay i finally fixed it, but now i have another problem! When i created my object it took those parameters and initialised them to it right? So why when i called their methods it returned a different value?

Because your Hero constructor isn't passing those values through to the Character constructor. Instead, it's storing them in some global variables you've declared in lines 2 - 6 of Hero.cpp. The same is true of your Monster class.
Last edited on
so do i declare them in my class as private member variables?
so do i declare them in my class as private member variables?

You already have - lines 6 - 10 of Characters.h

Incidentally, "Characters" is a misleading name for that class, since, as far as I can tell, each instance of it represents a single character.
Last edited on
So how can i pass those values through the character constructor?
This should be explained in whatever textbook you're using to learn C++.

1
2
3
4
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: Characters(newHp, newLevel, newAttack, newDef)
{
}


This assumes that the four arguments for the constructor you've declared at line 14 of Characters.h are the same four arguments as for Hero. You haven't named those arguments in the declaration, and you haven't shown us the definition of that constructor, so I can't be sure.
Topic archived. No new replies allowed.