Classes and Functions

Hello I was wondering because I know there is a better way to accomplish this.
I'm not asking for code, I would prefer if someone could tell me the name of the concept I need to study as an answer, or point me in the right direction.

I want to create a function that will compare a data member from 2 different instances of the same class and change one of the data members.

player1.p_Health -= player2.p_Attack;

This accomplishes just that if the members are public.

Is there a way where I can call 2 instances such as.

Player monster1("Goblin",50, 5);
Player monster2("Rat", 10, 2);

and have a single function I can call that will subtract the monster2's 10 from the monster1's 50.

The end goal here is to have a single function I can call that will take an argument of 2 different instances of 1 class to make changes to the data members.

Like monster1 attacking monster2 and reducing monster2's health by monster1's attack.

I'm guessing from what I've learned thus far that this would require overloading the function to take reference or pointer to the class's data members but when I try this I get alot of compiler errors.
If you use only one class (the Player class), the problem is greatly simplified:

1
2
3
4
5
6
7
8
9
10
11
12
void PerformAttack(const Player &attacker, Player &victim)
{
    //There's a small variety of choices here, being the least preferred the one you show in your post.
    //Here I present one that encapsulates better the data.
    victim.TakeHitFrom(attacker);
}
....
//where TakeHitFrom() is a member function of the Player class:
void Player::TakeHitFrom(const Player &attacker)
{
    p_Health -= attacker.p_Attack;
}


But if you have a variety of classes, what you need to study is Polymorphism.
Thank you for taking the time to respond webJose.
I wrote out the function and got it working now, I'm trying to expand upon it as well as reading the chapter about Polymorphism and Inheritance in my book. I got so caught up working on my own projects I stopped reading my books.


I have a second question.

In a game a class would have many data members, such as role playing games players have health, experience, attributes, resistances, equipment, inventory etc.

Is it proper to list all these out in a single class?
Example:

private:
unsigned short int p_Level;
long double p_Experience;
unsigned int p_Health;
unsigned int p_maxHealth;
unsigned short int p_Strength;
unsigned short int p_Attack;
short int p_Karma;
short int p_Reputation;
unsigned int p_Mana;
unsigned int p_maxMana;
unsigned short int p_Spirit;
unsigned short int p_Soul;
unsigned short int p_Defense;
unsigned short int p_Dexterity;
unsigned short int p_Agility;
unsigned short int p_Constitution;
unsigned short int p_Intelligence;
unsigned short int p_Wisdom;
unsigned short int p_Perception;
unsigned short int p_Charisma;
unsigned short int p_Stamina;
short int p_fireResist;
short int p_iceResist;
short int p_lightResist;
short int p_darkResist;
short int p_earthResist;
short int p_fireAttack;
short int p_iceAttack;
short int p_lightAttack;
short int p_darkAttack;
short int p_earthAttack;
unsigned long int p_Gold;

Or is there a more efficient/accepted way of doing this?
Since the end result is really not affected, it is up to programmer's taste. If you need the numbers, you need the numbers. It cannot be avoided.

I have never given this a second thought ever. If I need 20 member variables, I just declare them. I have, however, used internal data structures to hold together certain information. In C++ this is particularly useful because you could group your POD data inside a POD structure, and C++ will provide copy construction for this, therefore simplifying your tasks whenever you add/remove data from the class. But besides this, it is of little consequence, I would think.

Another way would be to just allocate a big byte array and map a POD to it, but really, I don't see much of an advantage.
I have a off the wall question. While I continue to work in the console window to test my code and work to improve/add to it I cannot help but wonder for the soon future.

Do you have any idea which would be more time consuming?

Programming 2D graphics in a graphics library such as Allegro or OpenGL
or
Programming 3D graphics in a graphics library such as Ogre3D?

I have always assumed programming 2D-Isometric graphics would be easier and simpler then rendering 3D graphics including shadows and lighting but some have been telling me as of late with the tools available today that programming 3D graphics is much easier then it used to be and even more so then most 2D libraries.
For either you need an artist. I can draw/generate simple (+ crapy) 2D icons, however 3D models are a whole different story.
Topic archived. No new replies allowed.