I have written the following program in C++
:
class A {
public:
A() {}
void foo() { cout << "A::foo" << endl; }
};
class B : public A {
public:
B() {}
void foo() override { cout << "B::foo" << endl; }
};
For some reason B::foo()
does not override the A::foo()
:
error: ‘void B::foo()’ marked ‘override’, but does not override
Also if I remove the override
keyword and run:
int main()
{
A* a = new B();
a->foo();
return 0;
}
It prints: A::foo
. Why it does not override?