Q. What is the output of the following C++ code?
Code:#include<iostream>
using namespace std;
class A {
public:
void show()
{ cout <<" show() method of class A"; }
};
class B : public A {
public:
void show()
{ cout <<" show() method of class B"; }
};
class C: public B {
};
int main(void)
{
C c;
c.show();
return 0;
}
β
Correct Answer: (B)
“show() method of class B”
Explanation: The show() method is not defined in the C class. It is therefore searched in the inheritance hierarchy. show() is present in both classes A and B, which one should be called? The idea is the following: if there is a multi-level inheritance, the function is then searched linearly in the inheritance hierarchy until a matching function is found.