Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. template<class T>
  10. class SmartPointer
  11. {
  12. public:
  13. T *id;
  14. SmartPointer(T *test)
  15. {
  16. id=test;
  17. cout<<"Nowy obiekt o id "<<endl;
  18. }
  19. ~SmartPointer()
  20. {
  21. cout<<"Destruktor obiektu"<<endl;
  22. }
  23. T* operator ->()
  24. {
  25. return id;
  26. }
  27. T& operator &()
  28. {
  29. return *id;
  30. }
  31. };
  32.  
  33. class Triangle
  34. {
  35. public:
  36. float x,y,z;
  37. Triangle(float x1,float y1,float z1)
  38. {
  39. cout<<"Nowy obiekt triangle"<<endl;
  40. this->x=x1;
  41. this->y=y1;
  42. this->z=z1;
  43. }
  44. float oblicz(unique_ptr<Triangle> &ptr)
  45. {
  46. return ptr->x+ptr->y+ptr->z;
  47. }
  48. };
  49.  
  50. class Triangle2
  51. {
  52. public:
  53. float x,y,z;
  54. Triangle2(float x1,float y1,float z1)
  55. {
  56. cout<<"Nowy obiekt triangle2"<<endl;
  57. this->x=x1;
  58. this->y=y1;
  59. this->z=z1;
  60. }
  61. float oblicz(shared_ptr<Triangle2> &ptr)
  62. {
  63. return ptr->x+ptr->y+ptr->z;
  64. }
  65. };
  66.  
  67. class WordBag
  68. {
  69. private:
  70. vector<unique_ptr<string>> vc;
  71. public:
  72. void add(unique_ptr<string> item)
  73. {
  74. vc.push_back(move(item));
  75. }
  76. unique_ptr<string> &take()
  77. {
  78. return vc.at(rand()%vc.size());
  79. }
  80. };
  81.  
  82. int main()
  83. {
  84. //2
  85. unique_ptr<Triangle>ptr(new Triangle(4,5,6));
  86. cout<<ptr->oblicz(ptr)<<endl<<endl;
  87. //3
  88. srand(time(0));
  89. unique_ptr<string> wskaznik(new string("Test"));
  90. unique_ptr<string> wskaznik2(new string("Tescik"));
  91. unique_ptr<string> wskaznik3(new string("Test123"));
  92.  
  93. WordBag wb=WordBag();
  94. wb.add(move(wskaznik));
  95. wb.add(move(wskaznik2));
  96. wb.add(move(wskaznik3));
  97. cout<<*wb.take()<<endl<<endl;
  98. //4
  99. shared_ptr<Triangle2>Xptr(new Triangle2(5,10,10));
  100. shared_ptr<Triangle2>Xptr1(Xptr);
  101. shared_ptr<Triangle2>Xptr2(Xptr1);
  102. cout<<Xptr2->oblicz(Xptr1)<<endl<<endl;
  103. return 0;
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement