fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Abstract
  6. {
  7. public:
  8. virtual void me() = 0;
  9. virtual Abstract * Clone() = 0;
  10. virtual ~Abstract(){}
  11. };
  12.  
  13. class Base: public Abstract
  14. {
  15. public:
  16. void me()
  17. {
  18. cout << "Base\n";
  19. }
  20. Abstract * Clone()
  21. {
  22. return new Base;
  23. }
  24. virtual ~Base(){}
  25. };
  26.  
  27. class Derived: public Base
  28. {
  29. public:
  30. void me()
  31. {
  32. cout << "Derived\n";
  33. }
  34. Derived * Clone()
  35. {
  36. return new Derived;
  37. }
  38. virtual ~Derived(){}
  39. };
  40.  
  41. void who(Abstract * a)
  42. {
  43. Abstract * b = a->Clone();
  44. b->me();
  45. delete b;
  46. };
  47.  
  48. int main()
  49. {
  50. Base * b = new Base;
  51. Derived * d = new Derived;
  52.  
  53. who(b);
  54. who(d);
  55.  
  56. delete b;
  57. delete d;
  58.  
  59. }
  60.  
  61.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Base
Derived