#include <iostream>
class base {
public:
virtual void print(){
std::cout << "base\n";
}
};
class dr : public base {
public:
void print(){
std::cout << "dr\n";
}
};
class last : public dr {
public:
void print(){
std::cout << "last\n";
}
};
int main(){
dr *d = new last();
d->print();
return 0;
}
In the above code, only the base
class includes a virtual function.
dr
and last
do not include a virtual function and yet polymorphism is still working.
The output of this code is last
. (It should have been dr
without polymorphism kicking in).
Does this mean polymorphism works as long as some base class has a virtual function, even though non of the derived classes have one?