fork download
  1. #include <iostream>
  2.  
  3. class Circle; class Rectangle;
  4. struct Shape {
  5. virtual void overlap(Shape* y) =0; //{ std::cout << "Circle, Shape" << std::endl; }
  6. virtual void overlap(Circle* x){};
  7. virtual void overlap(Rectangle* x){};
  8. };
  9.  
  10. struct Circle: Shape {
  11. void overlap(Shape* y) { y->overlap(this); }
  12. void overlap(Circle* y) { std::cout << "Circle, Circle" << std::endl; }
  13. void overlap(Rectangle* y) { std::cout << "Circle, Rectangle" << std::endl; }
  14. };
  15. struct Rectangle: Shape {
  16. void overlap(Shape* y) { y->overlap(this); }
  17. void overlap(Circle* y) { std::cout << "Rectangle, Circle" << std::endl; }
  18. void overlap(Rectangle* y) { std::cout << "Rectangle, Rectangle" << std::endl; }
  19. };
  20.  
  21. //void Shape::overlap(Circle*x) { x->overlap(this); }
  22. void overlap(Shape* x, Shape* y) { std::cout << "Shape, Shape" << std::endl; }
  23. void overlap(Circle* x, Shape* y) { std::cout << "Circle, Shape" << std::endl; }
  24. void overlap(Circle* x, Circle* y) { std::cout << "Circle, Circle" << std::endl; }
  25.  
  26. int main() {
  27. Shape* x = new Circle();
  28. Shape* y = new Circle();
  29. Shape* z = new Rectangle();
  30. std::cout<<"1-----"<<std::endl;
  31. x->overlap(y);
  32. overlap(x, y);
  33. std::cout<<"2-----"<<std::endl;
  34. x->overlap(z);
  35. overlap(x, z);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
1-----
Circle, Circle
Shape, Shape
2-----
Rectangle, Circle
Shape, Shape