0
why this output only comes 3 and why not output 123? #include<iostream> using namespace std; class A{ public: virtual void f(){cout<<1;} }; class B:public A{ public: void f(){cout<<2;} }; class C:public B{ public: void f(){cout<<3;} }; int main() { B *p=new C; p->f(); }
3 odpowiedzi
+ 2
This code output is 3. This is because p is a pointer of a C object, that has f() redefined in public. Does not return 123 because with the redefinition the code avoids the other f() versions.
Anyway, this kind of question has been resolved few times with posibly more elaborated answers. You only have to search by tag.
0
thanks Nestor
0
Nestor, what tag name I use to search for this question?