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.
//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;
elseif ((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);
};
//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);
};
//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;
}
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.
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?
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.
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.