-3

Greetings C++ community !

I am new to C++ i have this code example :

class Player
{
 public:
  int pdamage;
  int phealth;
 /* ... other data members and some void member functions (getName etc.) */
};

class Ennemy
{
 public:
  int edamage;
  int ehealth;
}; 

**/*here i would like to use current objects parameters for calculation and return to current player instance(current player object) the health value.*/**


int playerHit(this.Player.damage,this.Enemy.ehealth)
{
 ehealth = this.Ennemy.ehealth - this.Player.damage;
 return ehealth;
};

int ennemyHit(this.Player.phealth,this.Enemy.edamage)
{
 phealth = this.Player.phealth - this.Ennemy.edamage ;
 return ehealth;
};

int main()
{
/*....*/

return 0;
}

Returning to the post question: How i use current object parameters in a function for calculations?

/*Since i am new to stackoverflow and C++ Thanks for all advises and suggestions and critics ! */

Mir86
  • 21
  • 6
  • `int playerHit(this.Player.damage,this.Enemy.ehealth)` not C++ code, doesn';t match syntax rules – Swift - Friday Pie May 05 '18 at 10:08
  • 4
    Your code is very wrong. You should go back to a good book/tutorial about C++, especially the parts where it talks about classes. – Mat May 05 '18 at 10:08
  • in Java as i recall you can use this to reference for current object ,in C++ this is a pointer and * also is a pointer. Thats why in my head there are intereferences ,that is why i went to you guys ,to understand. – Mir86 May 05 '18 at 10:09
  • 2
    Sorry to say, but C++ is nothing like Java. There is no way to write a C++ program by guessing the syntax. You have to read up on how it works and can find [some good books here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Bo Persson May 05 '18 at 10:13
  • guys i know all that,go back to books etc. I prefer to code ,and learn by mistakes. – Mir86 May 05 '18 at 10:14
  • I think you wanna pass the objects of these two classes to the function. Since you have declared everything `public`, I think this might be the thing: `int playerHit(const Player& P, const Enemy& E) { return E.ehealth - P.pdamage; };`. And of-course, learn the basics before try something like this. – JeJo May 05 '18 at 10:15
  • JeJo thx for great answer i start to understand the concept. – Mir86 May 05 '18 at 10:18

1 Answers1

0

In C++ you would pass the calles either as pointer or reference

int playerHit(const Player& player, const Ennemy& ennemy)
{
     return ennemy.ehealth - player.pdamage;
};
FreshD
  • 2,914
  • 2
  • 23
  • 34