I have two classes:
class x {
public:
virtual void hello() {
std::cout << "x" << std::endl;
}
};
class y : public x {
public:
void hello() {
std::cout << "y" << std::endl;
}
};
Can someone explain why the following two calls to hello() print different messages? Why don't they both print "y"? Is it because the first one is a copy while the second one actually points to the object in memory?
int main() {
y a;
x b = a;
b.hello(); // prints x
x* c = &a;
c->hello(); // prints y
return 0;
}