I have the following code (simplified):
#include <cstdio>
class parent
{
public:
virtual void do_something() const
{
printf("hello I'm the parent class\n");
}
};
class child : public parent
{
public:
virtual void do_something() const
{
printf("hello I'm the child class\n");
}
};
void handle(parent p)
{
p.do_something();
}
int main()
{
child c;
handle(c);
return 0;
}
This prints hello I'm the parent class
, even though I passed a argument of type child
. How can I tell C++ to behave like Java does and invoke the method of the child, printing hello I'm the child class
?