#include <iostream>
class Animal
{
public:
virtual void message() { std::cout << "Hello from Animal" << std::endl; }
};
class Bird : public Animal
{
public:
void message() { std::cout << "Hello from Bird" << std::endl; }
};
int main()
{
Animal a = Bird();
a.message();
Animal* b = new Bird();
b->message();
delete b;
return 0;
}
Output:
Hello from Animal
Hello from Bird
I know that I have created an Animal object on the stack first, and on the heap later. However, why do they actually call different method implementations? Is there a way to call the base class message() function from the object b?