+ 4

Inheritance code reusability

In below code, why error of compilation is occurring instead of inheritance? class A { public: int f() {} int f(int a) {} }; class B : public A { public: int f() {} }; int main() { B b; b.f(7);// this causes error... return 0; } I believe method from A class should be available due to inheritance and it should build.

29th Mar 2018, 4:33 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 ответов
+ 2
It is but while you dont declare another method with same name (dont import params and return type) else you effectively hide base methods and only mode for call Base method from derived instance is: b.A::f(7)
29th Mar 2018, 5:04 PM
KrOW
KrOW - avatar
+ 1
does this mean if I don't write a method int f() in class B, code will compile properly?
29th Mar 2018, 5:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
correct... conclusion is that either define all or not a single function from all overloaded function of base class in derived class... thank you so much
29th Mar 2018, 5:29 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
👍👍👍👍
29th Mar 2018, 5:34 PM
KrOW
KrOW - avatar
0
Try it 😉
29th Mar 2018, 5:15 PM
KrOW
KrOW - avatar