Reputation: 1309
why its not printing 'doub'? Looking for a detailed explanation. Thanks for your time!
#include<iostream.h>
using namespace std;
class B{
public:
virtual int ft(int i) { cout <<"int"; return 0;}
};
class D: public B {
public:
double ft(double i){cout << "doub"; return 0.0;}
int ft(int i) { cout <<"intdoub"; return 0;}
};
int main(){
B *pB = new D;
pB->ft(2.3);
}
o/p is 'intdoub'
Upvotes: 1
Views: 172
Reputation: 25484
The variable pB
is of type B*
and does not know about the function double D::ft(double)
, only virtual int B::ft(int)
. The conversion of the double
value 2.3
to int
happens automatically, although you should have gotten a compiler warning.
Try:
dynamic_cast<D*>(pB)->ft(2.3);
dynamic_cast<D*>(pB)->B::ft(2.3);
Upvotes: 6