Having the following class hierarchy, what will be printed to the console?
#include <iostream>

using namespace std;

class A
{
  int m_i;
public:
  A(int i) : m_i(i) { }
  void print()
  {
    cout << sizeof(m_i) << endl;
  }
};

class B : virtual public A
{
public:
  B(int i) : A(i) { }
};

class C : public B
{
public:
  C(int i) : B(i) { }
};

int main()
{
  C c(1);
  c.print();
  return 0;
}
Explanation
Constructors are called first (explicitly or implicitly) from the bottom class on the hierarchy for virtual classes, in this case, class C. Since the constructor isn't invoked explicitly, then the default constructor is invoked implicitly, which is not defined in the class A. Hence the code will not compile.

Follow CodeGalaxy

Mobile Beta

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