Composite object and abstract class

Started by
13 comments, last by pulpfist 15 years, 6 months ago
Quote:Original post by dmatter
Quote:Original post by 3shirtlessmen
I could have 2 abstract weapons class, ones that have ammo and those that don't.
Weapon and ProjectileWeapon (or RangedWeapon)? That's one way, it'll certainly work.

Quote:Alright, I *thought* I did that ... still the abstract error on 'attack' ... why?
Are you suffering from overload versus override syndrome? Note the difference between these:

int Weapon::attack(int, int)

int LandMine::attack(int) // This is an overload NOT an override

You must override virtual functions in order to provide implementations for them.


Thanks!

Overriding - in this case, give int attack(int) a body in landmine? I did that - it compiles.

Also, why does it work that way? Reason being,

I assume that all weapons can 'attack' - however, not all weapons attack the same way. Which may lead to convoluted code.

My abstract super class allows for a attack(void); however, with my land mine - I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.
Advertisement
Quote:Original post by 3shirtlessmen
Quote:Original post by dmatter
int Weapon::attack(int, int)

int LandMine::attack(int) // This is an overload NOT an override

You must override virtual functions in order to provide implementations for them.


Thanks!

Overriding - in this case, give int attack(int) a body in landmine? I did that - it compiles.
Be sure that attack(int, int) has an implementation too though. You cannot have any unimplemented pure virtual functions if you want to instantiate a class.

Quote:Also, why does it work that way? Reason being,

I assume that all weapons can 'attack' - however, not all weapons attack the same way. Which may lead to convoluted code.

My abstract super class allows for a attack(void); however, with my land mine - I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.
Could you explain a little more? Part of the problem may be that I don't know what these integer parameters are that your functions are taking.

Perhaps you want a virtual int damage() = 0; function in the Weapon class, then specific weapons can override that to calculate their inflicting damage however they see fit.
Well, what's a good way to override it? I overloading it (the best way?) won't work, or rather, just complicates things.
Quote:I assume that all weapons can 'attack' - however, not all weapons attack the same way.


Please describe, in more detail, your concept of what "attacking" means, and what a "way of attacking" is. Design often requires talking things through.
Quote:
I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.


I don't see why you need 2 attack functions for that.
It is not the attack functions responsibility to set the weapons size is it?
The size of the LandMine sounds more like a LandMine constructor parameter

This topic is closed to new replies.

Advertisement