Composite object and abstract class

Started by
13 comments, last by pulpfist 15 years, 6 months ago
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]
Advertisement
LandMine doesn't provide overrides for all of the pure-virtual members that it inherits, meaning it too is abstract. You need to provide implementations somewhere for the pure virtual attack, reload and dropAmmo functions.
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.
Thanks!

Interesting ... is there a way to ONLY use attack()?

Or is this where clever design comes into play? :)

EDIT: source http://rafb.net/p/dOAhCF93.html
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.
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
int attack(int); is not the same as int attack(int, int);
Oh, I thought overloaded functions would have done that ... why don't they?
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.
Quote:Original post by 3shirtlessmen
Oh, I thought overloaded functions would have done that ... why don't they?
The overloaded functions exist along-side other functions of the same name, so int attack(int, int) is still left unimplemented.

This topic is closed to new replies.

Advertisement