1

Given 3 classes.

class vehicle{
public: 
void start();
}

class airplane: public vehicle{
void start();
void setRoute(route r);
void setAltitude(altitude a);
}

class boat: public vehicle{
void start();
void setRoute(route r);
}

Suppose we receive a parameter by command line that let us understand if we will manage an airplane or a boat.

Vehicle* v;


if (parameter == 1) {
   v = new airplane();
   v->setRoute(r);
   v->setALtitude(a);
}

if (parameter != 1) {
   v = new boat();
   v->setRoute(r);
}


v->start();

Note that different methods are called for boat and airplane before start. (Start is the only common method) This code will not work because v doesn't contains methods setRoute/SetALtitude.

What's the correct way of implement this pattern?

Overblade
  • 656
  • 2
  • 7
  • 25
Franconet
  • 41
  • 5
  • Sorry, write `if (parameter==1)` instead of `if (parameter=1)`... – ilotXXI Jul 13 '16 at 12:18
  • 2
    In this instance I would be tempted to make altitude part of the `route`. Otherwise if they have distinct abilities then you can give them distinct interfaces like `class flyable` and `class sailable` so a plain could be `class airplane: public vehicle, public flyable {}` and a boat `class boat: public vehicle, public sailable {}`... – Galik Jul 13 '16 at 12:58
  • but distinct interfaces doesn't allows me to use polymorphism, right? – Franconet Jul 13 '16 at 13:01
  • 3
    @Franconet Its not polymorphism when you try to control different interfaces from a common base (which is what you are trying to do here). Polymorphism is when multiple types share the same interface so you don't need to know the exact type to control them. So I would argue that having a common interface for each subvariety of vehicle is better polymorphism than trying to fudge a bunch of different acting types under a common all-encompasing interface. – Galik Jul 13 '16 at 13:21
  • 1
    If the subtypes are too different from one another then I would question if polymorphism is the best solution. – Galik Jul 13 '16 at 13:22
  • 1
    With polymorphism you can only control different subtypes to the extent they have things in common. Beyond that you have to know the more specific type. – Galik Jul 13 '16 at 13:23

2 Answers2

3

You can easily do:

if(parameter != 1)
{
    boat *b = new boat;
    b->setRoute(r);
    v = b;
}

and similar for airplane.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • What if I have this scenario? `if(parameter != 1) { boat *b = new boat; b->setRoute(r); v = b; } v.start(); /*Now I need to set something else*/ if(parameter != 1) { boat *c = new boat; b->SetSomethingElse(r); v = b; // route assignation is lost! }` – Franconet Jul 14 '16 at 08:26
  • @Franconet `boat *b = (boat*)v;`? (Or `boat *b = static_cast(v);` if you prefer that style of cast) – user253751 Jul 14 '16 at 08:56
0
class __declspec(novtable) vehicle{
public: 
    virtual void start();
    virtual void setRoute(route r) = 0;
    virtual void setAltitude(altitude a) = 0;
};

class airplane: public vehicle{
    virtual void start();
    virtual void setRoute(route r);
    virtual void setAltitude(altitude a);
};

class boat: public vehicle{
    virtual void start();
    virtual void setRoute(route r);
    virtual void setAltitude(altitude a);
};
RbMm
  • 31,280
  • 3
  • 35
  • 56