What will be printed out as a result of the execution?
class A
{
protected:
  virtual void v() = 0;

public:
  void f()
  {
    std::cout << "A::f()" << std::endl;
    this->v();
  }

protected:
  A()
  {
    std::cout << "A::A()" << std::endl;
    this->f();
  }
};

class B: public A
{
private:
  virtual void v()
  {
    std::cout << "B::v()" << std::endl;
  }
};

int main()
{
    A *pa = new B;
    pa->f();
    return 0;
}
Explanation
When the constructor of class A is executed, the function f is called, which calls the virtual function v. Since the mechanism of virtual functions does not work in the constructor and destructor, an attempt to call a pure virtual function will result in a "pure virtual function call" error and the program will terminate.

Follow CodeGalaxy

Mobile Beta

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