D

Dharmendra Sir • 3.53K Points
Extraordinary

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; 
}
  • (A) “show() method of class A”
  • (B) “show() method of class B”
  • (C) Both A and B are true
  • (D) None of these
  • Correct Answer - Option(B)
  • Views: 179
  • Filed under category C++

Explanation by: Dharmendra Sir
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.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics