vendredi 3 mai 2019

Whether virtual function coverage violates LSP?

I am learning design patterns, but I think C++ virtual function rewriting violates LSP.

1.Subclasses must implement abstract methods of the parent class, but must not override (override) the non-abstract (implemented) methods of the parent class.

But in order to achieve polymorphism, I have to rewrite(override). Is it that I understand it wrong?

class Animal;
class Cat;

void fun(Animal *xyz) { xyz->eat(); }

class Animal
{
 public:
    virtual void eat() { ::std::cout << "I'm eating generic food."; }
};

class Cat : public Animal
{
  public:
    // override.
    // Whether it violates the principle?
    void eat() { ::std::cout << "I'm eating a rat."; }
};

Aucun commentaire:

Enregistrer un commentaire