vendredi 1 juillet 2016

Multiple inheritance in oriented object C++ c++11

Here is the starting code. The question is : How would you define OR make a method call from overcraft class to produce a BEEP !

   class Vehicle {
public:
    virtual void honk() = 0;
};

class Car : public Vehicle {
public:
    void honk() { cout << "Beep!"; }
};

class Boat : public Vehicle {
public:
    void honk() { cout << "Toot!"; }
};

class Hovercraft : public Car, public Boat {
public:
    // ...
};

And here are the potentials solutions given to me : (it can be 1 to all answers possible)

  1. Add using Hovercraft::Car::honk; at a global level of the program or in the function where we want it to make the call

  2. add using Car::honk; in Hovercraft class

  3. add using Car::honk(); in Hovercraft class

  4. Car* c(new Hovercraft()); c->honk(); delete c;

  5. Hovercraft h; h.Car::honk();

  6. Hovercraft h; Car::h.honk();

  7. There is nothing to do, the inheritance order is anyway car::honk() called when we do honk() call from hovercraft

  8. Define in Overcraft a honk() method who call car::honk()

To me... 3 and 6 are false anyway. Am I right? I would pick 2 and 5 but when I submit these choices, the question is failed. I can try as much as I want but I would like to understand rather to try every combination...

Sorry for the translation mistake. Hope you will teach me some.

Aucun commentaire:

Enregistrer un commentaire