Classes/Objects. How do I make them attack? Please Help!!

Hi guys. So, I have this problem.

Problem:
I want to create two different classes. One class would represent
the "Hero" object. The other class would represent the "Enemy" object.

Now, with everything right, I would want to have these two objects fight each other. Well, how would I manipulate a console fight with texts, functions, and variables between two different classes?

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Enemy.h"

Enemy::Enemy()
{
    //ctor
}
int Enemy::attack()
{



}

void Enemy::hurt(int attack())
{
    health -= Enemy::attack();

}


This is one class. But, how will I be able to use a function of a different class to change the variable of a different class? Do you know what I'm trying to say?

Should I just inheritance? Like, make a "Unit class" that has HP variable and an "ATTACK" virtual function and use Polymorphism to create other classes derived from "Unit Class"

Please help!!!!!!
Why not just use one class and create two objects which have access to other variables within that class?
Last edited on
closed account (zb0S216C)
I would personally create a function in both classes called Attack( ) (or something like that). The method would take two parameters:

Parameter #1: The target. That is, a reference to the enemy/player receiving the damage.
Parameter #2: The damage magnitude.

Wazzak
Last edited on
^ ¿who will set that damage magnitude? ¿what will the function do?
Thanks guys. Danny, I thought of that too, but, I want to develop my Class making skills. I could make one Class but then later on I want to have many different monsters to battle.

Thank you Framework. I was thinking the same too, and I also tried to do the same, but, that' where' i was stuck on. I made two attack functions for each.

Here's what I'm doing. I made a class called "Unit". This Unit class is a base for the derived classes "Enemy" and "Hero". But the problem is, I don't quite know how to make the parameters work like you said. But I will keep trying.

ne555 yeah that's where I'm stuck on. I would just need help if someone could help me with the structure and build of it so I can use it as reference for future classes.
closed account (zb0S216C)
ne555 wrote:
¿what will the function do? (sic)

That's kinda' obvious.

OK, omit parameter #2. Here's a sample program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class BaseEnemy;

class Player
{
    public:
        Player( ) : Health( 100 ), AttackMag( 10 ) { }

        short Health;
        short AttackMag;
        void Attack( BaseEnemy & ); 
};

void Player::Attack( BaseEnemy &Target )
{
    Target.Health -= this->AttackMag;
    if( Target.Health < 0 )
        Target.Health = 0;
}

class BaseEnemy
{
    public:
        short Health;
        short AttackMag;

        virtual void Attack( Player & );
        // Additional information.
};

void BaseEnemy::Attack( Player &Target )
{
    Target.Health -= this->AttackMag;
    if( Target.Health < 0 )
        Target.Health = 0;
}

class Troll : public BaseEnemy
{
    public:
        Troll( ) : Health( 100 ), AttackMag( 10 ) { }

        void Attack( Player & );
        void SpecialAttack( Player & );
};

void Troll::Attack( Player &Target )
{
    Target.Health -= this->AttackMag;
    if( Target.Health < 0 )
        Target.Health = 0;
}

void Troll::SpecialAttack( Player &Target )
{
    Target.Health -= ( this->AttackMag * 3 );
    if( Target.Health < 0 )
        Target.Health = 0;
}

This was off the top of my head. Whether it works or not is questionable. It's quite linear, but it gives a rough focus.

Wazzak
Last edited on
Thank you, Framework!

So, I'm just trying to understand your code.

You used the reference to pass by value in the attack parameter, right?

Oh, and since there is only you, the Player, and enemies, you would have the BaseEnemy target "Player". And since enemies are enemies, you had the BaseEnemy as a base for Trolls, which is inheritance. Then you used a virtual function.

So, the attack function only has 1 parameter, right? The Parameter is a class?

Thanks again, Framework for helping me. Btw, what if we put the "health" as a private?
If that's the case then we would need to use a function accessor to access the variable right? Instead of this:

1
2
3
4
5
6
7
8
{
    void Troll::Attack( Player &Target )
{
    Target.Health -= this->AttackMag;
    if( Target.Health < 0 )
        Target.Health = 0;
}
}


instead of that, we would use something like this?

1
2
3
4
5
6
{
    void Troll::Attack( Player &Target )
{
    accesshealthdamage();
}
}


something like that? Thank you Framework !
closed account (zb0S216C)
hellohellomoon wrote:
You used the reference to pass by value in the attack parameter, right? (sic)

The reference is required so that the passed instantiation of Player will not be copied. If the instance was copied, the changes would not affect the original object. With the reference, the original passed instance would be affected.

hellohellomoon wrote:
So, the attack function only has 1 parameter, right? The Parameter is a class? (sic)

Yes, Player is a class. It accepts an instance of Player.

hellohellomoon wrote:
Btw, what if we put the "health" as a private? If that's the case then we would need to use a function accessor to access the variable right? (sic)

Yes, you could do that. The code I wrote was literally on-the-spot so there was no design.

hellohellomoon wrote:
instead of that, we would use something like this?
1
2
3
4
5
6
{
    void Troll::Attack( Player &Target )
{
    accesshealthdamage();
}
}
(sic)

As long as accesshealthdamage( ) deducts Target's health, then yeah, why not.

Wazzak
Last edited on
Thanks Framework. You thoroughly answered my question; like, you nailed it. Right now, I'm coding what you've taught me. It's working so far. You're awesome :)
Last edited on
then we would need to use a function accessor to access the variable right?
As long as accesshealthdamage( ) deducts Target's health, then yeah
Note the difference. If you use an accessor there will be the same as making the variable public.
However by providing a method that deals with all the logic (deducts Target's health) you are encapsulating properly (considerer death).

Another example: when you go to the market, you take your wallet, count the money and give it (the money) to the cashier.
Another scenario will be that the cashier ask for your wallet and takes the amount that wants

@Framework: ¿why are you overwriting a virtual function to do exactly the same?
Also, ¿could you please stop doing (sic) ? We trust that you will not misquote :)
Last edited on
Oh, I see ne555. Thanks :D yeah, my goal is to have all the variables private. My tutorials have all told me that this is the best way to do it. Hmm.. but I'm stuck on this one problem..

http://www.cplusplus.com/forum/beginner/52823/

I don't know why its like that?
closed account (zb0S216C)
ne555 wrote:
¿why are you overwriting a virtual function to do exactly the same? (sic)

It was an example. If hellohellomoon wanted to do a specific thing within that function then he/she can do so by overriding it. If he/she doesn't want to do anything specific with it, then normal health deduction occurs.

ne555 wrote:
Also, ¿could you please stop doing (sic) ? (sic)

That would be like me telling you to stop with the pointless upside-down question mark.

Wazzak
Last edited on
¡They are not pointless! Look closely ¿

By the way, if you have a virtual method you should make your destructor virtual.
Topic archived. No new replies allowed.