Sign in to follow this  
3shirtlessmen

Composite object and abstract class

Recommended Posts

Hey guys, I have an Event class, which is composed of a LandMine class, which is derived from an abstract class called Weapon ...
error C2259: 'LandMine' : cannot instantiate abstract class
class Event
{
private:
	string warning;
	int warningCount;

public:
	void displayWarning();
	LandMine EventTime;
};
class Weapon
{
protected:
	int damage;
	int ammoRemaining;

public:
	virtual int attack(int, int) = 0; // damage, ammoRemaining,
	virtual int reload(int) = 0; // USER_ammoAmount
	virtual void dropAmmo(int) = 0; //USER_ammoAmount
};
class LandMine : public Weapon
{
private:
	int size;

public:
	int attack(int); // OVERLOADED - Players remaining health
	void disarm();

	LandMine();

	~LandMine();
};
I just can't figure out why on earth this error is coming up. I've put virtual infront of the LandMine constructor too with no go. Ideas? Thanks Using VSC++ express sp1 64bit. EDIT: Source - http://rafb.net/p/dOAhCF93.html [Edited by - 3shirtlessmen on October 11, 2008 6:11:12 PM]

Share this post


Link to post
Share on other sites
In order to instantiate an object of a child class of an abstract base class, all the pure virtual member functions of that base class must be implemented. In your case LandMine needs to implement

virtual int attack(int, int) = 0; // damage, ammoRemaining,
virtual int reload(int) = 0; // USER_ammoAmount
virtual void dropAmmo(int) = 0; //USER_ammoAmount

Unless those functions are implemented you can't create a LandMine.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3shirtlessmen
Thanks!

Interesting ... is there a way to ONLY use attack()?
You could give the others 'empty' implementations - but don't do that [smile].

Quote:
Or is this where clever design comes into play? :)
Well, this is where good design comes into play. An interface is part of the specification. You've specified that a weapon can attack, reload and drop ammo. You've further specified that a land mine "is-a" type of weapon. By implication you should be saying that a land mine can attack, reload and drop ammo. Clearly that isn't what you want and it means you've made a mistake with your design.

The immediate flaw I see is the assumption that a weapon is reloadable and has ammo - land mines, knives, swords, rockets and broken bottles are all examples of weapons that don't fit those criteria.

Share this post


Link to post
Share on other sites
I think I understand.

I start broad with characteristics that ALL weapons have, and spread from there. Each tier down get's more specific. In this case, 2 tiers, a weapons class, and then the weapon.

I could have 2 abstract weapons class, ones that have ammo and those that don't.

EDIT:

Alright, I *thought* I did that ... still the abstract error on 'attack' ... why?

http://rafb.net/p/kaiI3S12.html

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this