Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Base
  6. {
  7. public:
  8. int *f;
  9. Base():f(new int)
  10. {
  11. *f = 12;
  12. }
  13. virtual ~Base()
  14. {
  15. delete f;
  16. }
  17. Base(const Base& p):f(new int)
  18. {
  19. *f = *p.f;
  20. }
  21. virtual Base* copy()
  22. {
  23. return new Base(*this);
  24. }
  25. };
  26.  
  27. class Child : public Base
  28. {
  29. public:
  30. Child()
  31. {
  32. *f = 22;
  33. }
  34. ~Child()
  35. {}
  36. Child(const Child& p)
  37. {
  38. *f = *p.f;
  39. }
  40. Child* copy()
  41. {
  42. return new Child(*this);
  43. }
  44. };
  45.  
  46.  
  47. int main()
  48. {
  49. vector<Base*> base;
  50. Base* b1 = new Base;
  51. Child* b2 = new Child;
  52. base.push_back(b1);
  53. base.push_back(b2);
  54. vector <Base*> v2;
  55. for (int i = 0; i < base.size(); i++)
  56. {
  57. Base* b = base[i] -> copy();
  58. v2.push_back(b);
  59. }
  60. for (int i = 0; i < base.size(); i++) {
  61. delete base[i];
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement