For the following code:
#include<iostream>
using namespace std;
class A{
public:
virtual int f(){return 1;}
};
class B : public A{
public:
virtual int f(){return 2;}
};
int main(int argc,char*argv[]){
A b=B();
cout<<b.f()<<endl;
}
I expect the number 2
to be printed. Instead the program prints the number 1
.
Could someone explain why this is?