I have problem with subclassing and using methods.
I create an instance of class B
and store it as a pointer to A
. But when I use the pointer to call the overloaded method, the output is "A" not "B". Why?
This works in other languages, what am I doing wrong?
#include <iostream>
using namespace std;
class A {
public:
void f() {
cout << "A";
}
};
class B : public A {
public:
void f() {
cout << "B";
}
};
int main() {
A *a = new B();
a->f();
return 0;
}
f()
method virtual. for to postpone binging at runtime. Otherwise static binging for – Lacework