Inheritance Quiz-Asked in Interviews

What will be the output of the following code snippet?

#include<iostream>using namespace std;class Base{public:void show(){cout<<" In Base ";}};class Derived: public Base{public:int x;void show(){cout<<"In Derived ";}Derived(){x = 10;}};int main(void){Base *bp, b;Derived d;bp = &d;bp->show();cout << bp->x;return 0;}

A) Compiler Error in line “ bp->show()”

B) Compiler Error in line “ cout << bp->x”

C) In Base 10

D)In Derived 10

Solution: B are Correct

A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer.

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Placewit

Placewit

Upskilling students for tech placements!