943,713 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 630
  • C++ RSS
Jan 11th, 2009
0

How to adjust class objects through seperate class functions???

Expand Post »
I want to create a battle system for an RPG game, one a little more robust then the previous one I made. This one I want to be very dynamic and easy to use. I want to use the Allegro API for it, but first I want to make sure I have everything going good in console before I put it into a real time rpg, but I'm having technical difficulties...

Now here are my classes...
C++ Syntax (Toggle Plain Text)
  1.  
  2. class Game{
  3. };
  4.  
  5. class Boxer:Game{
  6. public:
  7. char name[256];
  8. int health, *attack, level, critchance,xp,requiredxp;
  9. int Attack(){return -(*attack-rand()%(level/2));}
  10. void setName(){}
  11. void AdjustXP(int x){xp+=x;}
  12. void Spash(){printf("HP:%d\tXP:%d/%d\n",health,xp,requiredxp);}
  13. void EveryFrame(){*attack=level*3;}
  14. Boxer(){health=25;level=15;requiredxp=level*5,xp=0;}
  15. };
  16.  
  17.  
  18.  
  19. class Battle:Game{
  20. public:
  21. void Attack(int attackerhit, int * attackeehp){*attackeehp+=attackerhit;}
  22. };
  23.  
  24. int main(){
  25. srand(time(NULL));
  26. Boxer Boxer;
  27. Battle Battle;
  28. int attack=5;
  29. Battle.Attack(attack,Boxer->hp) //??? Heres my problem I believe
  30. cout<<Boxer.hp;
  31. }

Right now I want to pass the Boxer class' HP through a pointer to the Battle Classes Attack function, while passing a normal int (since the attack really doesnt need to be adjusted as of now). I get really weird errors like no matching function for call to `Battle::Battle(int&, int)'|... Please help!!!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

Well I can see a few issues with this code. First of all, when someone gets hit, i assume they would lose hp, not gain it. And as to the actual problem, the Boxer class doesn't contain a member called hp. The best way to do something like this is to make all the members in your classes private, then make some public accessor methods like this:

C++ Syntax (Toggle Plain Text)
  1. class Boxer:Game{
  2. private:
  3. char name[256];
  4. int health, *attack, level, critchance,xp,requiredxp;
  5. public:
  6. int Attack(){return -(*attack-rand()%(level/2));}
  7. void setName(){}
  8. void AdjustXP(int x){xp+=x;}
  9. void Spash(){printf("HP:%d\tXP:%d/%d\n",health,xp,requiredxp);}
  10. void EveryFrame(){*attack=level*3;}
  11. int GetHp() const
  12. {
  13. return health;
  14. }
  15. void SetHp(int const & hp)
  16. {
  17. health = hp;
  18. }
  19. Boxer(){health=25;level=15;requiredxp=level*5,xp=0;}
  20. };
  21.  
  22. class Battle:Game{
  23. public:
  24. void Attack(int const & attackerhit, Boxer & box){box.SetHp(box.GetHp() - attackerhit);}
  25. };

Voila. No pointers required. And much more dynamic as well.
Last edited by skatamatic; Jan 11th, 2009 at 5:10 pm.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

This might not seem dynamic, since a boxer is required as an argument rather than an arbitrary pointer, but I'm making the assumption that if you would like to include more types of boxers/etc that you would derive these classes from the basic boxer class, in which case you could make GetHP() and SetHP() polymorhpic (ie virtual methods). But you don't seem to be at this stage yet, so this should suffice.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

so far ty, however i have 2 questions, I dont know why I need a const in there, and I cant find any really helpful tuts on polymorphism and class deriving, can you lead me to a good one
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Jan 11th, 2009
1

Re: How to adjust class objects through seperate class functions???

so far ty, however i have 2 questions, I dont know why I need a const in there, and I cant find any really helpful tuts on polymorphism and class deriving, can you lead me to a good one
1. Everything that you don't want to modify in a function should have a const tag. And for the function itself, if you don't want to modify any of the classe's members inside a function, it is good practice to append a const modifier to the end of the function (as demonstrated with with GetHp).

2. I can't think of any good tutorials off the top of my head. Your best bet would be to take an intermediate c++ course. Or google polymorphism and base class inheritance.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

ugh can you just show me what you would do in the int main() too I'm really having severe problems, me and pointers are just not friends
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

C++ Syntax (Toggle Plain Text)
  1. int main(){
  2. srand(time(NULL));
  3. Boxer Boxer;
  4. Battle Battle;
  5. Battle.(Boxer.Attack(),Boxer) //solution
  6. cout<<Boxer.GetHp();
  7. }

This would work. But your code is fundamentally sloppy. Are you trying to teach c++ to yourself? I wouldn't recommend that, since c++ is the most complicated programming language in the world, in my opinion (next to Lisp). Try C# or VisualBasic. Or take a course on c++. Or if you're really addemant about teaching yourself, prepare for a long and frustrating battle.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 11th, 2009
0

Re: How to adjust class objects through seperate class functions???

Yes, I've been teaching myself for awhile. I'm fine with most aspects, its just pointers have always kicked my ass in one way or another, like the compiler will expect me to put something in that pointing to the pointer of something (**) and I'll google, not figure it out, then just post here. This is due to the fact that you guys rawk!!! thx much skata
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: casting
Next Thread in C++ Forum Timeline: how to count the char you input..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC