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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #1
Jan 11th, 2009
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...
  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!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

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

 
0
  #2
Jan 11th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

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

 
0
  #3
Jan 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #4
Jan 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

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

 
1
  #5
Jan 11th, 2009
Originally Posted by Manutebecker View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #6
Jan 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

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

 
0
  #7
Jan 11th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #8
Jan 11th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC