+ 4

Whats wrong with this C++ code...???

Code is correct but it gives compile time error... There is inheritance so base class method must be inherited in derived class which are public and protected but in this case that not happened. https://code.sololearn.com/cAQidii984bD/?ref=app

24th Dec 2017, 8:00 AM
Ajay Gaikwad
Ajay Gaikwad - avatar
4 Answers
+ 4
There is inheritance so base class method must be inherited in derived class which are public and protected but in this case that not happened. method of class A void f(int x) { cout<<x; } must be inherited in class B. // if I removed method of class B i.e. void f() { cout<<"b\n"; } Then object of class B able to call method of class A I.e. B ob; ob.f(7); // No error it works correctly.
24th Dec 2017, 8:23 AM
Ajay Gaikwad
Ajay Gaikwad - avatar
+ 3
Class B’s method doesn’t take arguments, so the compiler doesn’t know what to do with the 7
24th Dec 2017, 8:07 AM
Jared
Jared - avatar
0
add this void f(int x) { cout<<x; } after line 26 this function will be executed if you pass arguments with f
24th Dec 2017, 8:14 AM
‎ ‏‏‎Anonymous Guy
0
This is standart behavior. If derived class declares method with the same name as a method defined by base class, derived class' method hides base class' one. Solution: Add after public in class B: using A::f; or when call method ob.A::f(7);
24th Dec 2017, 9:56 AM
Kodirbek Makharov
Kodirbek Makharov - avatar