vendredi 29 juillet 2016

Why use base class pointers for derived classes

class base{
    .....
    virtual void function1();
    virtual void function2();
};

class derived::public base{
    int function1();
    int function2();
};

int main()
{
    derived d;
    base *b = &d;
    int k = b->function1() // Why use this instead of the following line?
    int k = d.function1(); // With this, the need for virtual functions is gone, right?

}

Sorry for a noobish question but I am not a CompSci engineer and I would like to know this. Why use virtual functions if we can avoid base class pointers?

Aucun commentaire:

Enregistrer un commentaire