+ 1
Why does this induce a compiler error?
class A{ public: int f(int x) {cout <<x; } }; class B: public A{ public: int() {cout <<5; } }; int main() { B ob; // new object created, so far this prints 5 right? ob.f(7); // won't this print 7 since it's calling the base class (class A's) function? } // outputs compiler error What caused the compiler error?
4 ответов
+ 4
Solus this is because function f() is a function which should return an integer but there is no return statement inside it.
+ 3
Solus here is the fixed code👇
https://code.sololearn.com/cBONIJ2MEZ01/?ref=app
+ 1
In public member block of class B:
int() { cout << 5; }
What was that? an anonymous function?
0
Basically a lack of return statements was the problem.
If there were return statements in both the base and derived classes, could we then expect an output of 57?