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’
The variable
pBis of typeB*and does not know about the functiondouble D::ft(double), onlyvirtual int B::ft(int). The conversion of thedoublevalue2.3tointhappens automatically, although you should have gotten a compiler warning.Try: