Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. // ConsoleApplication5.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>;
  6. using namespace std;
  7.  
  8. class Base{
  9. public:
  10. int *f ;
  11.  
  12. Base(){
  13. f = new int;
  14. *f=99;
  15. }
  16.  
  17. virtual ~Base(){
  18. delete f;
  19. }
  20.  
  21. virtual Base* copy(){
  22. return new Base(*this);
  23. }
  24.  
  25. Base(const Base & obj): f(new int){
  26. *f=*(obj.f);
  27. }
  28.  
  29. };
  30.  
  31. class Second:public Base{
  32. public:
  33.  
  34. Second(){
  35. *f=11;
  36. }
  37.  
  38. ~Second(){
  39.  
  40. }
  41. };
  42.  
  43. int _tmain(int argc, _TCHAR* argv[])
  44. {
  45. Base* a= new Base;
  46. Base* b= new Second ;
  47. vector <Base*> v;
  48. v.push_back( a );
  49. v.push_back ( b );
  50.  
  51. vector <Base*> v2;
  52.  
  53. for (int i=0; i < v.size(); i++){
  54. Base* b= v[i]->copy();
  55. v2.push_back(b);
  56. }
  57.  
  58. for (int i=0; i<v.size(); i++){
  59. delete v[i];
  60. }
  61.  
  62. for (int i=0; i<v2.size(); i++){
  63. delete v2[i];
  64. }
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement