What will be printed out as a result of the following code execution?
#include "stdio.h"

class Parent {
  public:
    void GetValue() { Count(); }
  private:
    virtual void Count() { printf("%d", 1); }
};

class Child : public Parent { 
  private:
    void Count() { printf("%d", 2); }
};

int main() {
  Parent * obj = new Child;
  obj->GetValue();
  return 0;
}
Explanation
There is no compilation error because the GetValue() method has access to the Count() method of the base class. But instead of the Count() from base class, the method of descendant is called polymorphically.

Follow CodeGalaxy

Mobile Beta

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