Reputation: 14418
I have a derived derived class from an abstract class. The code is below. I have a FishTank class which is derived from an Aquarium and Aquarium is derived from item. My question is that should I put the definition of virtual int minWidth() const = 0; in aquarium again or is the code below sufficient?
class Item{
public:
virtual int minWidth() const = 0;
};
class Aquarium: public Item{
public:
virtual int calWidth() = 0; // Pure virtual function.
};
class FishTank : public Aquarium{
public:
FishTank(int base1, int base2, int height);
~FishTank();
int calWidth();
int minWidth();
};
Upvotes: 0
Views: 304
Reputation: 504313
There's no reason to do it again. It only serves to waste space and give you the opportunity to get compile errors from typos. :) Once you inherit, it's just like it had been there anyway.
However, you don't actually ever implement it! Why? You're missing const
in FishTank
:
int minWidth() const; // <-- const!
Upvotes: 6
Reputation: 793249
Do you mean "definition" or just a "declaration"? If a suitable default definition is likely to be applicable to all Aquariums then by all means define it in the Aquarium class, but you might not want to make it pure virtual otherwise all derived classes would still have to override it even if they just wanted to call the base class implementation.
Otherwise, there's no point in just re-declaring it. Either way derived concrete classes must still override the function and it makes no difference the behaviour of the function when called through a pointer or reference to either Item or Aquarium.
Upvotes: 1
Reputation: 888223
You don't need to re-declare minWidth
; the pure virtual function will be inherited from Item
.
Upvotes: 1