I have two classes:
class A {
int get();
string str();
}
string A::str() {
return string_with( get() );
}
class B : public A{
int get();
}
if I do:
A a
B b
b.str()
b will invoke A::str() (good) and it ill use A::get() method (bad!). I want, that when I invoke b.str(), the B::get() is used by str.
How to make it work?
Just make it virtual. That’s exactly what virtual is for.
Write
in A’s definition. And, just to make the code more understandable, do the same in B’s.
By the way, I’m assuming that you meant to write
class B : public A.