My C++ Quiz

The ultimate place for testing what you have learned



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

Question #56: What gets printed by this program?
57% on 901 times asked

#include <iostream>

struct Shape
{
  virtual void print()
  {
    std::cout << "SHAPE" << std::endl;
  }
  virtual ~Shape() {}
};

struct Box : public virtual Shape
{
  void print()
  {
    std::cout << "BOX" << std::endl;
  }
};

struct Sphere : public virtual Shape
{
  void print()
  {
    std::cout << "SPHERE" << std::endl;
  }
};

struct GeoDisc : public Box, public Sphere
{
};

int main(int argc, char** argv)
{
  Shape* s = new GeoDisc;

  s->print();

  delete s;

  return 0;
}

SHAPE
BOX
SPHERE
undefined
code is ill-formed
© 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