My C++ Quiz

The ultimate place for testing what you have learned



Home | Start Quiz | List of questions  | Next Question | Login / Register

Question #59: What gets printed by this program?
67% on 850 times asked

#include <iostream>

struct Shape
{
  virtual Shape* duplicate()
  {
    std::cout << "SHAPE" << std::endl;
    return new Shape;
  }
  virtual ~Shape() {}
};

struct Box : public Shape
{
  virtual Box* duplicate()
  {
    std::cout << "BOX" << std::endl;
    return new Box;
  }
};

int main(int argc, char** argv) 
{ 
  Shape* s1 = new Box;

  Shape* s2 = s1->duplicate();

  delete s1;
  delete s2;
  return 0; 
}

SHAPE
BOX
undefined
code is ill-formed
unspecified
© 2007-2010, My C++ Quiz, All rights reserved.
Can you find a bug in the quiz? Can you think of a new question that would be cool to add to this site? Email us and we will take action:
email us
Other Quizes | Contributors