+ 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?

27th Jul 2020, 12:27 AM
Solus
Solus - avatar
4 ответов
+ 4
Solus this is because function f() is a function which should return an integer but there is no return statement inside it.
27th Jul 2020, 12:34 AM
Arsenic
Arsenic - avatar
27th Jul 2020, 3:33 AM
Arsenic
Arsenic - avatar
+ 1
In public member block of class B: int() { cout << 5; } What was that? an anonymous function?
27th Jul 2020, 1:35 AM
Ipang
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?
27th Jul 2020, 1:20 AM
Solus
Solus - avatar