What will be displayed as a result of the program execution?
#include <iostream>

using namespace std;

struct B
{
  void f() { cout << "B"; }
};

struct C: B
{
  void f() { cout << "C"; }
};

int main()
{
  C c;

  B *b1 = &c;
  b1->f();

  B b2 = c;
  b2.f();
  return 0;
}
Explanation
Method B::f() is not a virtual method, so when you access it through the pointer to the object of the base class, this method is called, not the overridden method of the child class.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes