+ 2

C++ challenge from quizes

Whats the output and how did you get it ? class A{ public:   void f() {cout<<1;} }; class B:public A{ private:   void f() {cout<<2;} }; void g(A &a) {a.f();} int main{ B b; g(b); }

5th Aug 2017, 2:38 PM
Marko Majstorovic
Marko Majstorovic - avatar
2 Respuestas
+ 8
Neglecting minor syntax errors: class B is derived from class A. We have a function g which accepts an object of class A as it's parameter and calls method f() using object of class A. Now in main(), we create an object of class B. We pass that object into function g, which accepts the object without errors (because class B is derived from class A). Now, function g treats this object as a class A object, so it calls f() from class A. // prints 1
5th Aug 2017, 3:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
You are better than SoloLearn ;)
5th Aug 2017, 3:07 PM
Marko Majstorovic
Marko Majstorovic - avatar