Re: Can I inherit to reuse code?
When is an abstract base class, except interfaces, necessary anyway?
With a pure abstract class (interface) you separate interface and
implementation; with a concrete (nonabstract at all) base class you
stablish an "is a" relationship (e.g. Dog is an Animal); with an
abstract base class you do both.
That's true. Usually I use following construction instead of using an
abstract base class.
class CarInterface
{
public:
virtual void turnLeft() = 0;
virtual void turnRight() = 0;
};
class DefaultCar : public CarInterface
{
public:
DefaultCar();
virtual void turnLeft() { ... }
virtual void turnRight() { ... }
};
With this construction anyone that just wants to get up running with a
Car can inherit from DefaultCar, and those who want to build a super car
can create their own from scratch with the interface.
My first question is: is this a normal construction in the industry?
Now, the original reason to this post was that I found out that I want
to configure the default car.
class DefaultCar : public CarInterface
{
public:
DefaultCar();
virtual void turnLeft() { ... }
virtaul void turnRigth() { ... }
protected:
void setEngine_( int engine );
};
class MyCar : public DefaultCar
{
public:
MyCar() { setEngine_( MONSTER_V12 ); }
};
What do you think of this kind of initialization construction?
Don't hesitate to give bad criticism, this has to be right from the
beginning. Suggestions to alternatives are of course also welcome.
Thanks,
Daniel
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]